示例#1
0
    //==========helper functions=================
    void doTestAll(const char* soFile)
    {
        ClassLoader *loader = new ClassLoader(soFile);

        void *test;
        
        TS_ASSERT_THROWS_NOTHING((TestInterface *)loader->createInstance("TestReader", test));
        TS_ASSERT(test != NULL);
        if(test) ((TestInterface *)test)->hello();
        delete test;

        TS_ASSERT_THROWS_NOTHING((TestInterface *)loader->createInstance("TestWriter", test));
        TS_ASSERT(test != NULL);
        if(test) ((TestInterface *)test)->hello();
        delete test;

        TS_ASSERT_THROWS_NOTHING((TestInterface *)loader->createInstance("TestTemplate<int>", test));
        TS_ASSERT(test != NULL);
        if(test) ((TestInterface *)test)->hello();
        delete test;

        TS_ASSERT_THROWS_NOTHING((TestInterface *)loader->createInstance("TestTemplate<float>", test));
        TS_ASSERT(test != NULL);
        if(test) ((TestInterface *)test)->hello();
        delete test;

        TS_ASSERT_THROWS((TestInterface *)loader->createInstance("TestXXX", test),ClassLoaderError);

        delete loader;
    }
示例#2
0
void ClassLoaderTest::testClassLoader3()
{
	std::string path = "TestLibrary";
	path.append(SharedLibrary::suffix());

	ClassLoader<TestPlugin> cl;
	cl.loadLibrary(path);
	cl.loadLibrary(path);
	cl.unloadLibrary(path);
	
	assert (cl.manifestFor(path).size() == 3);
	
	ClassLoader<TestPlugin>::Iterator it = cl.begin();
	assert (it != cl.end());
	assert (it->first == path);
	assert (it->second->size() == 3);
	++it;
	assert (it == cl.end());
	
	cl.unloadLibrary(path);
	assertNullPtr (cl.findManifest(path));
}
示例#3
0
void ClassLoaderTest::testClassLoader1()
{
	std::string path = "TestLibrary";
	path.append(SharedLibrary::suffix());

	ClassLoader<TestPlugin> cl;

	assert (cl.begin() == cl.end());
	assertNullPtr (cl.findClass("PluginA"));
	assertNullPtr (cl.findManifest(path));
	
	assert (!cl.isLibraryLoaded(path));
	
	try
	{
		const ClassLoader<TestPlugin>::Meta& meta = cl.classFor("PluginA");
		fail("not found - must throw exception");
	}
	catch (NotFoundException&)
	{
	}
	catch (...)
	{
		failmsg("wrong exception");
	}

	try
	{
		const ClassLoader<TestPlugin>::Manif& manif = cl.manifestFor(path);
		fail("not found - must throw exception");
	}
	catch (NotFoundException&)
	{
	}
	catch (...)
	{
		failmsg("wrong exception");
	}
}
示例#4
0
void ClassLoaderTest::testClassLoader2()
{
	std::string path = "TestLibrary";
	path.append(SharedLibrary::suffix());

	ClassLoader<TestPlugin> cl;
	cl.loadLibrary(path);

	assert (cl.begin() != cl.end());
	assertNotNullPtr (cl.findClass("PluginA"));
	assertNotNullPtr (cl.findClass("PluginB"));
	assertNotNullPtr (cl.findClass("PluginC"));
	assertNotNullPtr (cl.findManifest(path));
	
	assert (cl.isLibraryLoaded(path));
	assert (cl.manifestFor(path).size() == 3);
	
	ClassLoader<TestPlugin>::Iterator it = cl.begin();
	assert (it != cl.end());
	assert (it->first == path);
	assert (it->second->size() == 3);
	++it;
	assert (it == cl.end());
	
	TestPlugin* pPluginA = cl.classFor("PluginA").create();
	assert (pPluginA->name() == "PluginA");
	assert (!cl.classFor("PluginA").isAutoDelete(pPluginA));
	delete pPluginA;

	TestPlugin* pPluginB = cl.classFor("PluginB").create();
	assert (pPluginB->name() == "PluginB");
	delete pPluginB;
	
	pPluginB = cl.create("PluginB");
	assert (pPluginB->name() == "PluginB");
	delete pPluginB;
	
	assert (cl.canCreate("PluginA"));
	assert (cl.canCreate("PluginB"));
	assert (!cl.canCreate("PluginC"));

	TestPlugin& pluginC = cl.instance("PluginC");
	assert (pluginC.name() == "PluginC");
	
	try
	{
		TestPlugin& plgB = cl.instance("PluginB");
		fail("not a singleton - must throw");
	}
	catch (InvalidAccessException&)
	{
	}
	
	try
	{
		TestPlugin* pPluginC = cl.create("PluginC");
		fail("cannot create a singleton - must throw");
	}
	catch (InvalidAccessException&)
	{
	}

	try
	{
		const AbstractMetaObject<TestPlugin>& meta = cl.classFor("PluginC");
		meta.autoDelete(&(meta.instance()));
		fail("cannot take ownership of a singleton - must throw");
	}
	catch (InvalidAccessException&)
	{
	}
	
	const AbstractMetaObject<TestPlugin>& meta1 = cl.classFor("PluginC");
	assert (meta1.isAutoDelete(&(meta1.instance())));

	// the following must not produce memory leaks
	const AbstractMetaObject<TestPlugin>& meta2 = cl.classFor("PluginA");
	meta2.autoDelete(meta2.create());
	meta2.autoDelete(meta2.create());

	TestPlugin* pPlugin = meta2.create();
	meta2.autoDelete(pPlugin);
	assert (meta2.isAutoDelete(pPlugin));
	meta2.destroy(pPlugin);
	assert (!meta2.isAutoDelete(pPlugin));

	cl.unloadLibrary(path);
}
/*
 * Class:     java_lang_VMClassRegistry
 * Method:    getSystemPackages
 * Signature: (I)[[Ljava/lang/String;
 */
JNIEXPORT jobjectArray JNICALL Java_java_lang_VMClassRegistry_getSystemPackages
  (JNIEnv *jenv, jclass, jint len)
{
    Global_Env* genv = VM_Global_State::loader_env;
    ClassLoader* cl = static_cast<ClassLoader*>
        (genv->bootstrap_class_loader);
    Package_Table* ptab = cl->getPackageTable();
    cl->Lock();
    unsigned p_num = (unsigned)ptab->size();
    if (p_num == (unsigned)len) 
    {
        cl->Unlock();
        return NULL;
    }
    const char ** pkgs = (const char **)STD_MALLOC(p_num * 2 * sizeof(char*));
    size_t buf_len = 0;
    unsigned index = 0;
    for (Package_Table::const_iterator it = ptab->begin(), end = ptab->end(); 
        it != end; ++it)
    {
        const char* name = pkgs[index++] = (*it).first->bytes;
        pkgs[index++] = (*it).second->get_jar();
        size_t name_len = (*it).first->len;
        if (name_len > buf_len) {
            buf_len = name_len;
        }
    }
    cl->Unlock();

    jclass string_class = struct_Class_to_java_lang_Class_Handle(genv->JavaLangString_Class);
    static Class* aos = genv->LoadCoreClass("[Ljava/lang/String;");
    jclass string_array_class = struct_Class_to_java_lang_Class_Handle(aos);
    assert(string_class);
    assert(string_array_class);
        
    jobjectArray result = NewObjectArray(jenv, p_num, string_array_class, NULL);
    if (result) 
    {
        char* buf = (char*)STD_MALLOC(buf_len + 1);
        p_num *= 2;
        for (index = 0; index < p_num; )
        {
            jobjectArray pair = NewObjectArray(jenv, 2, string_class, NULL);
            if (!pair) {
                break;
            }
            SetObjectArrayElement(jenv, result, index/2, pair);

            char* name = strcpy(buf, pkgs[index++]);
            for (char* c = name; *c != '\0'; ++c) {
                if (*c == '/') {
                    *c = '.';
                }
            }
            jstring jname = NewStringUTF(jenv, name);
            if (!jname) {
                break;
            }
            SetObjectArrayElement(jenv, pair, 0, jname);

            const char * jar = pkgs[index++];
            if (jar) {
                jstring js = NewStringUTF(jenv, jar);
                if (!js) break;
                SetObjectArrayElement(jenv, pair, 1, js);
            }
        }
        STD_FREE(buf);
    }

    STD_FREE(pkgs);
    
    assert(result || exn_raised());
    return result;
}
JNIEXPORT void JNICALL
Java_java_lang_ClassLoader_registerInitiatedClass(JNIEnv* env, jobject loader, jclass clazz) {
    ClassLoader* cl = class_loader_lookup(loader);
    Class* clss = jclass_to_struct_Class(clazz);
    cl->InsertInitiatedClass(clss);
}
Object AbstractContextI::lookupObjectFactory(const String& name,
											 bool& createdbyfactory,
											 String& redirectedname) const throw (NamingException)
{
	try
	{
		redirectedname = name;
		createdbyfactory = false;
		Object object = lookupWithSyntaxCheck(name, false);
		StringAnything referencelink;
		if (object->instanceOf(referencelink))
		{
			if (isBound(referencelink->toString()))
			{
				object = lookupURL(referencelink->toString());
				redirectedname = referencelink->toString();
			}
		}
		MapAnything objectfactory;
		if (object->instanceOf(objectfactory))
		{
			if (objectfactory->containsKey(L"class") && objectfactory->containsKey(L"library"))
			{
				bool load = true;
				if (objectfactory->containsKey(L"load")) load = objectfactory->get(L"load");
				if (load)
				{
					ClassLoader classloader;
					String clazzname = objectfactory->get(L"class");

					Anything library = objectfactory->get(L"library");
					if (Anything::ANY_STRING == library->type())
					{
						StringBuffer buf = library->toString();
						if (0 == buf->indexOf(L"http://"))
						{
							classloader = new URLClassLoaderI();
						}
						else
						{
							classloader = new ClassLoaderI();
						}
						classloader->loadLibrary(library);
					}
					else
					{
						if (Anything::ANY_LIST == library->type())
						{
							classloader = new URLClassLoaderI();
							Iterator<Anything> i = library->iterator();
							while (i->hasNext())
							{
								String library = i->next();
								classloader->loadLibrary(library);
							}
						}
					}
					object = classloader->loadClass(clazzname)->newInstance();
					const_cast<AbstractContextI*>(this)->rebind(name, object);
					createdbyfactory = true;
					const_cast<AbstractContextI*>(this)->bind(
						name + L"/" + name->toMD5(), new StringAnythingI(redirectedname));
				}
			}
		}
		else
		{
			createdbyfactory = isBound(name + L"/" + name->toMD5());
			if (createdbyfactory) redirectedname = lookup(name + L"/" + name->toMD5())->toString(); 
		}
		return object;
	}
	catch (const NamingException&)
	{
		throw;
	}
	catch (const Exception& e)
	{
		throw NamingException(WITHDETAILS(e->toString()));;
	}
}