Incidentally, you can load the JDBC driver at runtime; you don't have to
have it on the CP. You'll have to wrap it with a shim loaded by the
system CL, though, because DriverManager will choke on the non-system
CL, for whatever reason. http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
Toss the shim'd driver in to the DriverManager, and pass null to Cayenne
for the driver (which tells it to ask DriverManager for a driver), and
you should be good to go. You can see an example of my DataSourceFactory
here:
http://bnubot.googlecode.com/svn/trunk/BNUBot/src/net/bnubot/db/conf/Cay
enneConfiguration.java
-----Original Message-----
From: rigel [mailto:zetata..mail.com]
Sent: Monday, November 24, 2008 3:05 PM
To: use..ayenne.apache.org
Subject: Re: Loading cayenne mapped class from URLClassLoader
Hi Andrus, first of all thank you for your great work :clap: .
Andrus Adamchik wrote:
>
> Hi, could you possibly try it with Cayenne 3.0M4? I think it should
> already be fixed there.
>
Yes of course, I try to put my small contribute for what I can.
This is the output (JDBC driver is the only jar pre-loaded into
classpath
because differently by Cayenne 2 now (DriverDataSource.java:62) does not
solve the
class driver):
Loading lib: http://localhost:80/users/antares/libs/ashwood-2.0.jar
Loading lib: http://localhost:80/users/antares/libs/asm-3.0.jar
Loading lib: http://localhost:80/users/antares/libs/asm-commons-3.0.jar
Loading lib:
http://localhost:80/users/antares/libs/commons-collections-3.1.jar
Loading lib:
http://localhost:80/users/antares/libs/commons-logging-1.1.jar
Loading lib:
http://localhost:80/users/antares/libs/geronimo-jpa_3.0_spec-1.0.jar
Loading lib: http://localhost:80/users/antares/libs/velocity-1.3.jar
Loading lib:
http://localhost:80/users/antares/libs/cayenne-agent-3.0M4.jar
Loading lib:
http://localhost:80/users/antares/libs/cayenne-client-3.0M4.jar
Loading lib:
http://localhost:80/users/antares/libs/cayenne-modeler-3.0M4.jar
Loading lib:
http://localhost:80/users/antares/libs/cayenne-server-3.0M4.jar
Loading lib: http://localhost:80/users/antares/libs/w3d-base.jar
Loading lib: http://localhost:80/users/antares/libs/w3d-orm.jar
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
startedLoading
INFO: started configuration loading.
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
shouldLoadDataDomain
INFO: loaded domain: dbw3d
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
loadDataMap
INFO: loaded .
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
shouldLoadDataNode
INFO: loading .
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
shouldLoadDataNode
INFO: using factory: org.apache.cayenne.conf.DriverDataSourceFactory
24-nov-2008 20.20.13 org.apache.cayenne.conf.DriverDataSourceFactory
load
INFO: loading driver information from 'dbw3dNode.driver.xml'.
24-nov-2008 20.20.13
org.apache.cayenne.conf.DriverDataSourceFactory$DriverHandler init
INFO: loading driver org.postgresql.Driver
24-nov-2008 20.20.13
org.apache.cayenne.conf.DriverDataSourceFactory$LoginHandler init
INFO: loading user name and password.
java.lang.ClassNotFoundException:
org.apache.cayenne.conf.PlainTextPasswordEncoder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at
org.apache.cayenne.conn.DataSourceInfo.getPasswordEncoder(DataSourceInfo
.java:207)
at
org.apache.cayenne.conf.DriverDataSourceFactory$LoginHandler.init(Driver
DataSourceFactory.java:324)
at
org.apache.cayenne.conf.DriverDataSourceFactory$DriverHandler.startEleme
nt(DriverDataSourceFactory.java:191)
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElemen
t(Unknown
Source)
at
com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emp
tyElement(Unknown
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanSta
rtElement(Unknown
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$F
ragmentContentDriver.next(Unknown
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unkn
own
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Un
known
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s
canDocument(Unknown
Source)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unkn
own
Source)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unkn
own
Source)
at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown
Source)
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unkno
wn
Source)
at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.pars
e(Unknown
Source)
at
org.apache.cayenne.conf.DriverDataSourceFactory.load(DriverDataSourceFac
tory.java:130)
at
org.apache.cayenne.conf.DriverDataSourceFactory.getDataSource(DriverData
SourceFactory.java:78)
at
org.apache.cayenne.conf.RuntimeLoadDelegate.shouldLoadDataNode(RuntimeLo
adDelegate.java:299)
at
org.apache.cayenne.conf.ConfigLoader$NodeHandler.init(ConfigLoader.java:
337)
at
org.apache.cayenne.conf.ConfigLoader$DomainHandler.startElement(ConfigLo
ader.java:223)
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElemen
t(Unknown
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanSta
rtElement(Unknown
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$F
ragmentContentDriver.next(Unknown
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unkn
own
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Un
known
Source)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s
canDocument(Unknown
Source)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unkn
own
Source)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unkn
own
Source)
at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown
Source)
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unkno
wn
Source)
at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.pars
e(Unknown
Source)
at
org.apache.cayenne.conf.ConfigLoader.loadDomains(ConfigLoader.java:79)
at
org.apache.cayenne.conf.DefaultConfiguration.initialize(DefaultConfigura
tion.java:164)
at
org.apache.cayenne.conf.Configuration.initializeSharedConfiguration(Conf
iguration.java:157)
at
org.apache.cayenne.conf.Configuration.initializeSharedConfiguration(Conf
iguration.java:139)
at
org.apache.cayenne.conf.Configuration.initializeSharedConfiguration(Conf
iguration.java:119)
at
org.apache.cayenne.conf.Configuration.getSharedConfiguration(Configurati
on.java:89)
at
org.apache.cayenne.access.DataContext.createDataContext(DataContext.java
:162)
at it.wipidea.w3d.SomeClass.init(SomeClass.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at NetLoader.start(NetLoader.java:65)
at NetLoader.main(NetLoader.java:76)
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger
logPoolCreated
INFO: Created connection pool: jdbc:postgresql://localhost/w3d
Driver class: org.postgresql.Driver
Min. connections in the pool: 1
Max. connections in the pool: 1
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
shouldLoadDataNode
INFO: loaded datasource.
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
initAdapter
INFO: no adapter set, using automatic adapter.
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
shouldLinkDataMap
INFO: loaded map-ref: dbw3dMap.
24-nov-2008 20.20.13 org.apache.cayenne.conf.RuntimeLoadDelegate
finishedLoading
INFO: finished configuration loading in 142 ms.
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger logQueryStart
INFO: --- will run 1 query.
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger logConnect
INFO: Opening connection: jdbc:postgresql://localhost/w3d
Login: postgres
Password: *******
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger
logConnectSuccess
INFO: +++ Connecting: SUCCESS.
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger
logBeginTransaction
INFO: --- transaction started.
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger log
INFO: Detected and installed adapter:
org.apache.cayenne.dba.postgres.PostgresAdapter
24-nov-2008 20.20.13 org.apache.cayenne.access.QueryLogger logQueryError
INFO: *** error.
java.lang.ClassNotFoundException: it.weev.orm.cayenne.dbw3d.matlibs
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.cayenne.util.Util.getJavaClass(Util.java:589)
at
org.apache.cayenne.map.ObjEntity.getJavaClass(ObjEntity.java:271)
at
org.apache.cayenne.reflect.PersistentDescriptorFactory.getDescriptor(Per
sistentDescriptorFactory.java:57)
at
org.apache.cayenne.reflect.ClassDescriptorMap.createDescriptor(ClassDesc
riptorMap.java:129)
at
org.apache.cayenne.reflect.LazyClassDescriptorDecorator.checkDescriptorI
nitialized(LazyClassDescriptorDecorator.java:51)
at
org.apache.cayenne.reflect.LazyClassDescriptorDecorator.getEntity(LazyCl
assDescriptorDecorator.java:75)
at
org.apache.cayenne.access.trans.SelectTranslator.appendQueryColumns(Sele
ctTranslator.java:267)
at
org.apache.cayenne.access.trans.SelectTranslator.buildResultColumns(Sele
ctTranslator.java:240)
at
org.apache.cayenne.access.trans.SelectTranslator.createSqlString(SelectT
ranslator.java:103)
at
org.apache.cayenne.dba.postgres.PostgresSelectTranslator.createSqlString
(PostgresSelectTranslator.java:32)
at
org.apache.cayenne.access.trans.QueryAssembler.createStatement(QueryAsse
mbler.java:116)
at
org.apache.cayenne.access.jdbc.SelectAction.performAction(SelectAction.j
ava:72)
at
org.apache.cayenne.access.DataNodeQueryAction.runQuery(DataNodeQueryActi
on.java:58)
at
org.apache.cayenne.access.DataNode.performQueries(DataNode.java:230)
at
org.apache.cayenne.access.DataDomainQueryAction.runQuery(DataDomainQuery
Action.java:424)
at
org.apache.cayenne.access.DataDomainQueryAction.access$000(DataDomainQue
ryAction.java:68)
at
org.apache.cayenne.access.DataDomainQueryAction$2.transform(DataDomainQu
eryAction.java:397)
at
org.apache.cayenne.access.DataDomain.runInTransaction(DataDomain.java:84
7)
at
org.apache.cayenne.access.DataDomainQueryAction.runQueryInTransaction(Da
taDomainQueryAction.java:394)
at
org.apache.cayenne.access.DataDomainQueryAction.execute(DataDomainQueryA
ction.java:120)
at
org.apache.cayenne.access.DataDomain.onQuery(DataDomain.java:740)
at
org.apache.cayenne.util.ObjectContextQueryAction.runQuery(ObjectContextQ
ueryAction.java:317)
at
org.apache.cayenne.util.ObjectContextQueryAction.execute(ObjectContextQu
eryAction.java:96)
at
org.apache.cayenne.access.DataContext.onQuery(DataContext.java:1336)
at
org.apache.cayenne.access.DataContext.performQuery(DataContext.java:1325
)
at it.wipidea.w3d.SomeClass.init(SomeClass.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at NetLoader.start(NetLoader.java:65)
at NetLoader.main(NetLoader.java:76)
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at NetLoader.start(NetLoader.java:65)
at NetLoader.main(NetLoader.java:76)
Caused by: org.apache.cayenne.CayenneRuntimeException: [v.3.0M4 May 18
2008
15:29:33] Query exception.
at
org.apache.cayenne.access.DataDomainQueryAction.nextQueryException(DataD
omainQueryAction.java:551)
at
org.apache.cayenne.access.DataNode.performQueries(DataNode.java:237)
at
org.apache.cayenne.access.DataDomainQueryAction.runQuery(DataDomainQuery
Action.java:424)
at
org.apache.cayenne.access.DataDomainQueryAction.access$000(DataDomainQue
ryAction.java:68)
at
org.apache.cayenne.access.DataDomainQueryAction$2.transform(DataDomainQu
eryAction.java:397)
at
org.apache.cayenne.access.DataDomain.runInTransaction(DataDomain.java:84
7)
at
org.apache.cayenne.access.DataDomainQueryAction.runQueryInTransaction(Da
taDomainQueryAction.java:394)
at
org.apache.cayenne.access.DataDomainQueryAction.execute(DataDomainQueryA
ction.java:120)
at
org.apache.cayenne.access.DataDomain.onQuery(DataDomain.java:740)
at
org.apache.cayenne.util.ObjectContextQueryAction.runQuery(ObjectContextQ
ueryAction.java:317)
at
org.apache.cayenne.util.ObjectContextQueryAction.execute(ObjectContextQu
eryAction.java:96)
at
org.apache.cayenne.access.DataContext.onQuery(DataContext.java:1336)
at
org.apache.cayenne.access.DataContext.performQuery(DataContext.java:1325
)
at it.wipidea.w3d.SomeClass.init(SomeClass.java:45)
... 6 more
Caused by: java.lang.ClassNotFoundException:
it.weev.orm.cayenne.dbw3d.matlibs
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.cayenne.util.Util.getJavaClass(Util.java:589)
at
org.apache.cayenne.map.ObjEntity.getJavaClass(ObjEntity.java:271)
at
org.apache.cayenne.reflect.PersistentDescriptorFactory.getDescriptor(Per
sistentDescriptorFactory.java:57)
at
org.apache.cayenne.reflect.ClassDescriptorMap.createDescriptor(ClassDesc
riptorMap.java:129)
at
org.apache.cayenne.reflect.LazyClassDescriptorDecorator.checkDescriptorI
nitialized(LazyClassDescriptorDecorator.java:51)
at
org.apache.cayenne.reflect.LazyClassDescriptorDecorator.getEntity(LazyCl
assDescriptorDecorator.java:75)
at
org.apache.cayenne.access.trans.SelectTranslator.appendQueryColumns(Sele
ctTranslator.java:267)
at
org.apache.cayenne.access.trans.SelectTranslator.buildResultColumns(Sele
ctTranslator.java:240)
at
org.apache.cayenne.access.trans.SelectTranslator.createSqlString(SelectT
ranslator.java:103)
at
org.apache.cayenne.dba.postgres.PostgresSelectTranslator.createSqlString
(PostgresSelectTranslator.java:32)
at
org.apache.cayenne.access.trans.QueryAssembler.createStatement(QueryAsse
mbler.java:116)
at
org.apache.cayenne.access.jdbc.SelectAction.performAction(SelectAction.j
ava:72)
at
org.apache.cayenne.access.DataNodeQueryAction.runQuery(DataNodeQueryActi
on.java:58)
at
org.apache.cayenne.access.DataNode.performQueries(DataNode.java:230)
... 18 more
This is my netloader code:
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.TreeMap;
public final class NetLoader {
final static private ResourceBundle RB;
static {
RB = ResourceBundle.getBundle(
//"loader-2_0_4"
"loader-3_M4"
);
}
private URL[] libUrls;
public NetLoader() {
this.initLibrary();
}
private void initLibrary() {
TreeMap libs = new TreeMap();
Enumeration en = RB.getKeys();
while (en.hasMoreElements()) {
String key = en.nextElement();
libs.put(key,RB.getString(key).split(","));
}
libUrls = new URL[libs.size()];
Iterator it = libs.keySet().iterator();
int idx = 0;
while (it.hasNext()) {
String key = it.next();
URL url = null;
try {
url = new
URL(libs.get(key)[0],libs.get(key)[1],libs.get(key)[2]);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
System.exit(1);
}
libUrls[idx++] = url;
int port = (url.getPort()==-1) ? 80 :
url.getPort() ;
System.out.println("Loading lib: " +
url.getProtocol()+"://"+url.getHost()+":"+port+url.getFile());
}
}
protected void start() {
URLClassLoader urlClassLoader = new
URLClassLoader(libUrls,
Thread.currentThread().getContextClassLoader()
//ClassLoader.getSystemClassLoader()
);
try {
Class<?> clzz =
Class.forName("it.wipidea.w3d.SomeClass",true,urlClassLoader);
//Class<?> clzz =
urlClassLoader.loadClass("it.wipidea.w3d.SomeClass");
Method m = clzz.getDeclaredMethod("init", new
Class[]{});
m.invoke(null, new Object[]{});
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
*..aram args
*/
public static void main(String[] args) {
NetLoader netLoader = new NetLoader();
netLoader.start();
}
}
And this is the property file used to load all libs:
## Cayenne III
#libs.001 =
http,localhost,/users/antares/libs/postgresql-8.2-504.jdbc4.jar
libs.011 = http,localhost,/users/antares/libs/ashwood-2.0.jar
libs.012 = http,localhost,/users/antares/libs/asm-3.0.jar
libs.013 = http,localhost,/users/antares/libs/asm-commons-3.0.jar
libs.014 =
http,localhost,/users/antares/libs/commons-collections-3.1.jar
libs.015 = http,localhost,/users/antares/libs/commons-logging-1.1.jar
libs.016 =
http,localhost,/users/antares/libs/geronimo-jpa_3.0_spec-1.0.jar
libs.017 = http,localhost,/users/antares/libs/velocity-1.3.jar
libs.021 = http,localhost,/users/antares/libs/cayenne-agent-3.0M4.jar
libs.022 = http,localhost,/users/antares/libs/cayenne-client-3.0M4.jar
libs.023 = http,localhost,/users/antares/libs/cayenne-modeler-3.0M4.jar
libs.024 = http,localhost,/users/antares/libs/cayenne-server-3.0M4.jar
libs.031 = http,localhost,/users/antares/libs/w3d-base.jar
libs.032 = http,localhost,/users/antares/libs/w3d-orm.jar
## Cayenne II
#libs.001 =
http,localhost,/users/antares/libs/postgresql-8.2-504.jdbc4.jar
#libs.011 = http,localhost,/users/antares/libs/cayenne-2_0_4-util.jar
#libs.021 = http,localhost,/users/antares/libs/w3d-base.jar
#libs.022 = http,localhost,/users/antares/libs/w3d-orm.jar
Currently I am not able to pass all Maven test to build Cayenne,
so I don't know if the "Class.forName(className)" allows the Util.class
to solve all classes in the URLLoaderClasspath.
Hoping can be useful, sorry for long long post :wistle: :-P.
Best Regards.
Andrus Adamchik wrote:
>
> Hi, could you possibly try it with Cayenne 3.0M4? I think it should
> already be fixed there.
>
> Cheers,
> Andrus
>
>
>>
>> --
>> View this message in context:
>>
http://www.nabble.com/Loading-cayenne-mapped-class-from-URLClassLoader-t
p20654024p20655214.html
>> Sent from the Cayenne - User mailing list archive at Nabble.com.
>
>
>
-- View this message in context: http://www.nabble.com/Loading-cayenne-mapped-class-from-URLClassLoader-t p20654024p20668667.html Sent from the Cayenne - User mailing list archive at Nabble.com.
This archive was generated by hypermail 2.0.0 : Mon Nov 24 2008 - 15:20:12 EST