If your Spring wiring is being loaded, that should work.  One word of
caution is that you should not use this type of strategy in any static
initialization blocks or anywhere that it could be loaded prior to the
Spring ApplicationContext load completing.

-----Original Message-----
From: Dan Olsen [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, June 03, 2008 2:41 PM
To: Jetspeed Users List
Subject: RE: Accessing Jetspeed Database

So I have the following line of code:
 
DataSource ds = DataSourceFactory.getInstance();
 
Should I now be able to call ds.getConnection()? I do this and I am
getting errors saying my connection is null.

>>> "Tim Garrett" <[EMAIL PROTECTED]> 6/3/2008 10:44 AM >>>

When I have to bridge that gap between Spring and non-Spring classes, I
have usually created a Factory with a static getter and a non-static
setter (or use constructor injection).  Then, I would wire a bean in
Spring to construct an instance of the factory and call the non-static
setter.  The setter would set the value of the static instance element.
Then, you could just call DataSourceFactory.getDataSource() to get the
instance.  It saves you from replicating a lot of Spring setup that
already works perfectly.  I haven't tried this exact thing in Jetspeed
yet, but I have done it in other similar environments.

Example: 

public class DataSourceFactory {
    private static DataSource instance;

    public static DataSource getInstance() { return
DataSourceFactory.instance; }
    
    public void setInstance(DataSource ds) {
        DataSourceFactory.instance = ds;
    }
}

Supplementary Spring config (in etc/assembly/override)

<bean id="dataSourceFactory" class="DataSourceFactory">
    <property name="instance" ref="JetspeedDS" /> </bean>

Then, DataSourceFactory.getInstance() would return the Spring
"singleton" object for the data source.

-----Original Message-----
From: Dan Olsen [mailto:[EMAIL PROTECTED]
Sent: Tuesday, June 03, 2008 11:23 AM
To: Jetspeed Users List
Subject: RE: Accessing Jetspeed Database

Can I access that in a non-Spring class?

>>> "Tim Garrett" <[EMAIL PROTECTED]> 6/3/2008 9:44 AM >>>

To the best of my knowledge, Jetspeed is wiring it via Spring in
WEB-INF/assembly/boot/datasource.xml.  The Spring bean is called
"JetspeedDS".

Tim Garrett
Software Engineer
Saepio Technologies
[EMAIL PROTECTED]
816.777.2158
Saepio Makes Distributed Marketing Effective, Engaging & Easy.

-----Original Message-----
From: Dan Olsen [mailto:[EMAIL PROTECTED]
Sent: Tuesday, June 03, 2008 10:38 AM
To: Jetspeed Users List <jetspeed-user@portals.apache.org
Subject: Accessing Jetspeed Database

I am trying to access the Jetspeed database. There is a jetspeed.xml
file in {tomcat_dir}/conf/Catalina/localhost/. I know Jetspeed can
access the database but I can't seem to get access to it. Does anyone
know how Jetspeed is setting up the database connection? When I try the
following code, I get a naming exception. If anyone knows why that code
does not get the work please let me know ASAP. Thanks!

==== BEGIN MY getConnection FUNCTION ==== public static Connection
getConnection(RenderResponse response)
   throws IOException {
  // TODO Auto-generated method stub
  try {
   Context initCtx = new InitialContext();
   Context envCtx = (Context) initCtx.lookup("java:/comp/env");
   if (envCtx == null) {
    throw new Exception("Boom - No Environment Context");
   }
   // envCtx.1
   DataSource ds = (DataSource) envCtx.lookup("jdbc/jetspeed");
   if (ds != null) {
    return ds.getConnection();
   } else {
    return null;
   }
  } catch (NamingException ex) {
   ex.printStackTrace(response.getWriter());
  } catch (Exception ex) {
   ex.printStackTrace(response.getWriter());
  }

  return null;
}

==== END MY getConnection FUNCTION ====

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED] 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED] 
For additional commands, e-mail: [EMAIL PROTECTED] 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to