This is a weblogic startup application that creates a jdo and puts
it in the jndi tree
package vanderbilt.ed.utils;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.util.Logger;
import vanderbilt.ed.utils.EDWhiteboardLog;
import weblogic.common.T3ServicesDef;
import weblogic.common.T3StartupDef;
public class ConfigureJDO implements T3StartupDef
{
public String _wlTransactionManager =
"weblogic.transaction.TransactionManager";
public String _databaseType = "oracle";
public String _jndiName = "ConfigureJDO";
public String _databaseConfigFile;
public String _jndiProvider;
public T3ServicesDef _services;
public String _loggingKey = "ConfigureJDO.startup()";
public InitialContext _context;
public JDO _jdo;
public Logger _logger;
public ConfigureJDO()
{
}
public void setServices(T3ServicesDef services)
{
_services = services;
}
public String startup(String name, Hashtable args) throws
Exception
{
_loggingKey = "ConfigureJDO.startup()";
if (!(args != null && args.containsKey("databaseConfigFile")
&& args.containsKey("databaseType")))
{
EDWhiteboardLog.info("ConfigureJDO: invalid parameters");
return null;
}
_jndiProvider = (String)args.get("jndiProvider");
_databaseConfigFile = (String)args.get("databaseConfigFile");
_databaseType = (String)args.get("databaseType");
EDWhiteboardLog.info("ConfigurJDO:jndiProvider = " +
_jndiProvider);
EDWhiteboardLog.info("databaseConfigFile = " +
_databaseConfigFile);
EDWhiteboardLog.info("databaseType = " + _databaseType);
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, _jndiProvider);
_context = new InitialContext(ht);
//file:/C:/bea/wlserver6.1/config/edisdomain/serverclasses/database
Pool.xml>
EDWhiteboardLog.info("Location of databasePool.xml "
+
(String)getClass().getResource(_databaseConfigFile).toString());
// PrintWriter writer = new
Logger(System.out).setPrefix(jndiName);
ClassLoader klassLoader = this.getClass().getClassLoader();
_jdo = new JDO();
_logger = new Logger( System.out ).setPrefix( "castor" );
_jdo.setLogWriter(_logger);
// jdo.setLogWriter(writer);
_jdo.setDatabaseName(_databaseType);
_jdo.setConfiguration(getClass().getResource(_databaseConfigFile).t
oString());
_jdo.setTransactionManager(_wlTransactionManager);
_jdo.setClassLoader(klassLoader);
// register JDO in JNDI
EDWhiteboardLog.info("Binding JDO to JNDI: " + _jndiName);
try
{
EDWhiteboardLog.info("JDO Reference: " + _jdo);
_context.rebind(_jndiName, _jdo.getReference());
}
catch (Exception e)
{
EDWhiteboardLog.error("ConfigureJDO :", e);
}
return "JDO created and bound to jndi";
}
}
//////////////////////////////////////////////////
This is an example of a base dao which loads the mapping and
database files and gets the jdo object from the jndi tree. Put the
mapping and database files in both your WEB-INF/classes and
serverclasses directory. It needs to be in a location where all
the classloaders can get to it.
import java.io.Serializable;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.DatabaseNotFoundException;
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.util.Logger;
import org.exolab.castor.mapping.Mapping;
import vanderbilt.ed.utils.*;
/**
* @author Kevin Lanaghan
*/
/* access modes
Exclusive;
ReadOnly;
DbLocked;
*/
public class BaseDAO implements Serializable
{
public final static String DATABASE_FILE =
"/applications/EDWhiteboard/databasePool.xml";
public final static String MAPPING_FILE =
"/applications/EDWhiteboard/mapping.xml";
public final static String DATABASE_TYPE = "oracle";
protected String _databaseFilePath;
protected Mapping _mapping;
public String _mappingFilePath;
public static final String Usage = "Usage: example jdo";
protected JDO _jdo;
protected Database _db;
private static Logger _logger;
public void init(String databaseFilePath, String MappingFilePath)
{
boolean retry = true;
try
{
Context context = new InitialContext();
_jdo = (JDO)context.lookup("ConfigureJDO");
_db = _jdo.getDatabase();
// if(_db != null)
// _db.setAutoStore(true);
return;
}
catch (DatabaseNotFoundException e)
{
EDWhiteboardLog.error("**** BaseDAO constructor : DB not
found", e);
}
catch (Exception e)
{
EDWhiteboardLog.error("**** BaseDAO constructor Fatal
error", e);
}
}
public BaseDAO() throws Exception
{
init(DATABASE_FILE, MAPPING_FILE);
}
public BaseDAO(String databaseFilePath, String MappingFilePath)
throws Exception
{
init(databaseFilePath, MappingFilePath);
}
public BaseDAO(Database db) throws Exception
{
if (db != null)
{
_db = db;
}
else
{
throw new Exception("BaseDAO:BaseDAO(Database db) _db passed
to BaseDAO Constructor is null");
}
}
public Database getDb()
{
return _db;
}
public void begin() throws Exception
{
EDWhiteboardLog.info("BaseDAO:begin(): START ");
if (_db != null)
_db.begin();
else
EDWhiteboardLog.info("BaseDAO:begin(): db is null ");
EDWhiteboardLog.info("BaseDAO:begin(): SUCCESSFUL ");
}
public void close()
{
try
{
EDWhiteboardLog.info("BaseDAO:close(): START ");
if (_db != null && !(_db.isClosed()))
// if (_db != null && _db.isActive())
{
_db.close();
EDWhiteboardLog.info("BaseDAO:close(): SUCCESSFUL ");
}
else
{
EDWhiteboardLog.info("BaseDAO:close(): db is already
closed: ");
}
}
catch (Exception e)
{
EDWhiteboardLog.error("Can't close dao: " + e.toString(), e);
}
}
public void commit() throws Exception
{
if (_db != null && _db.isActive())
{
EDWhiteboardLog.info("BaseDAO: commit(): START");
_db.commit();
EDWhiteboardLog.info("BaseDAO: commit(): SUCCESS");
}
else
{
if(_db == null)
EDWhiteboardLog.info("BaseDAO: commit failed: DB is null ");
if(!(_db.isActive()) )
EDWhiteboardLog.info("BaseDAO: commit failed: Transaction
is not active ");
}
}
public void rollback()
{
try
{
EDWhiteboardLog.info("START: BaseDAO: Rollback ");
if (_db != null && _db.isActive())
{
_db.rollback();
EDWhiteboardLog.info("SUCCESS: BaseDAO: Rollback ");
}
else
{
if(_db == null)
EDWhiteboardLog.info("BaseDAO: Rollback failed: DB is
null ");
if(!(_db.isActive()) )
EDWhiteboardLog.info("BaseDAO: Rollback failed:
Transaction is not active ");
}
}
catch (Exception e)
{
EDWhiteboardLog.error("BaseDAO: Rollback failed ", e);
}
}
public Object load(Class xclass, long id) throws Exception
{
Object obj = null;
if (_db != null && _db.isActive())
obj = _db.load(xclass, new Long(id));
return obj;
}
public void remove(Object rec) throws Exception
{
if (_db != null && _db.isActive())
_db.remove(rec);
}
}
--On Thursday, September 26, 2002 10:09 AM -0400 "Marcelo Flores
A." <[EMAIL PROTECTED]> wrote:
>
> Friends please.. URGENT
>
> I need a example with Configuration of JDO Initialization
> with JNDI.
> Atte. Marcelo Flores
> [EMAIL PROTECTED]
> Aditiva S.A.
---------------------------------------
Lanaghan, Kevin M
Vanderbilt University
Email: [EMAIL PROTECTED]
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev