Persistence

Session.java# getSessionFromProto NullPointerException korrigiert.

git-svn-id: https://svn.libreccm.org/ccm/trunk@590 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2010-10-28 12:31:46 +00:00
parent 53e63abbc3
commit 5751398d42
1 changed files with 110 additions and 111 deletions

View File

@ -68,7 +68,6 @@ import org.apache.log4j.Logger;
public class Session { public class Session {
private static final Logger LOG = Logger.getLogger(Session.class); private static final Logger LOG = Logger.getLogger(Session.class);
private MetadataRoot m_root; private MetadataRoot m_root;
private ConnectionSource m_source; private ConnectionSource m_source;
private int m_database; private int m_database;
@ -76,7 +75,6 @@ public class Session {
private PSession m_ssn; private PSession m_ssn;
private final RDBMSEngine m_engine; private final RDBMSEngine m_engine;
private final QuerySource m_qs; private final QuerySource m_qs;
private List m_dataObjects = new ArrayList(); private List m_dataObjects = new ArrayList();
FlushEventProcessor m_beforeFP; FlushEventProcessor m_beforeFP;
FlushEventProcessor m_afterFP; FlushEventProcessor m_afterFP;
@ -88,35 +86,35 @@ public class Session {
m_database = database; m_database = database;
com.redhat.persistence.engine.rdbms.ConnectionSource src = com.redhat.persistence.engine.rdbms.ConnectionSource src =
new com.redhat.persistence.engine.rdbms.ConnectionSource() { new com.redhat.persistence.engine.rdbms.ConnectionSource() {
private Connection m_conn = null; private Connection m_conn = null;
public Connection acquire() { public Connection acquire() {
m_conn = m_source.acquire(m_conn); m_conn = m_source.acquire(m_conn);
return m_conn; return m_conn;
} }
public void release(Connection conn) { public void release(Connection conn) {
m_source.release(conn); m_source.release(conn);
} }
}; };
CompoundProfiler profs = new CompoundProfiler(); CompoundProfiler profs = new CompoundProfiler();
profs.add(m_prof); profs.add(m_prof);
profs.add(new DeveloperSupportProfiler()); profs.add(new DeveloperSupportProfiler());
switch (m_database) { switch (m_database) {
case DbHelper.DB_ORACLE: case DbHelper.DB_ORACLE:
m_engine = new RDBMSEngine(src, new OracleWriter(), profs); m_engine = new RDBMSEngine(src, new OracleWriter(), profs);
break; break;
case DbHelper.DB_POSTGRES: case DbHelper.DB_POSTGRES:
m_engine = new RDBMSEngine(src, new PostgresWriter(), profs); m_engine = new RDBMSEngine(src, new PostgresWriter(), profs);
break; break;
default: default:
DbHelper.unsupportedDatabaseError("persistence"); DbHelper.unsupportedDatabaseError("persistence");
m_engine = null; m_engine = null;
break; break;
} }
m_qs = new QuerySource(); m_qs = new QuerySource();
@ -131,12 +129,17 @@ public class Session {
m_ssn.addAfterFlush(m_afterFP); m_ssn.addAfterFlush(m_afterFP);
m_ssn.addBeforeDelete(new EventProcessor() { m_ssn.addBeforeDelete(new EventProcessor() {
protected void cleanUp(boolean isCommit) { }
protected void flush() { } protected void cleanUp(boolean isCommit) {
}
protected void flush() {
}
protected void write(Event ev) { protected void write(Event ev) {
if (ev instanceof DeleteEvent) { if (ev instanceof DeleteEvent) {
BeforeDeleteEvent bde = BeforeDeleteEvent bde =
new BeforeDeleteEvent((DataObjectImpl) ev.getObject()); new BeforeDeleteEvent((DataObjectImpl) ev.getObject());
bde.fire(); bde.fire();
} }
} }
@ -145,8 +148,8 @@ public class Session {
Root r = m_root.getRoot(); Root r = m_root.getRoot();
synchronized (r) { synchronized (r) {
Adapter ad = r.getAdapter(DataObjectImpl.class); Adapter ad = r.getAdapter(DataObjectImpl.class);
if (ad == null || if (ad == null
!(ad instanceof DataObjectAdapter)) { || !(ad instanceof DataObjectAdapter)) {
ad = new DataObjectAdapter(); ad = new DataObjectAdapter();
r.addAdapter(DataObjectImpl.class, ad); r.addAdapter(DataObjectImpl.class, ad);
r.addAdapter(PropertyMap.class, ad); r.addAdapter(PropertyMap.class, ad);
@ -233,18 +236,15 @@ public class Session {
* *
* @return The transaction context for this Session. * @return The transaction context for this Session.
**/ **/
public TransactionContext getTransactionContext() { public TransactionContext getTransactionContext() {
return m_ctx; return m_ctx;
} }
/** /**
* Returns the JDBC connection associated with this session. * Returns the JDBC connection associated with this session.
* *
* @return The JDBC connection used by this Session object. * @return The JDBC connection used by this Session object.
**/ **/
public Connection getConnection() { public Connection getConnection() {
return m_engine.getConnection(); return m_engine.getConnection();
} }
@ -266,16 +266,14 @@ public class Session {
* @see #create(String) * @see #create(String)
**/ **/
public DataObject create(ObjectType type) { public DataObject create(ObjectType type) {
if (type == null) { if (type == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("type must be non null");
("type must be non null"); }
}
DataObjectImpl result = new DataObjectImpl(type); DataObjectImpl result = new DataObjectImpl(type);
result.setSession(m_ssn); result.setSession(m_ssn);
return result; return result;
} }
/** /**
* Creates and returns an empty DataObject of the given type. The * Creates and returns an empty DataObject of the given type. The
* properties in the data object may then be initialized using * properties in the data object may then be initialized using
@ -303,17 +301,14 @@ public class Session {
* @return A persistent object of the type identified by * @return A persistent object of the type identified by
* <i>typeName</i>. * <i>typeName</i>.
**/ **/
public DataObject create(String typeName) { public DataObject create(String typeName) {
ObjectType type = m_root.getObjectType(typeName); ObjectType type = m_root.getObjectType(typeName);
if (type == null) { if (type == null) {
throw new PersistenceException throw new PersistenceException("no such type: " + typeName);
("no such type: " + typeName); }
}
return create(type); return create(type);
} }
/** /**
* Creates a new DataObject with the type of the given oid and initializes * Creates a new DataObject with the type of the given oid and initializes
* the key properties to the values specified in the oid. * the key properties to the values specified in the oid.
@ -321,7 +316,6 @@ public class Session {
* @param oid The OID that specifies the type of and key properties for * @param oid The OID that specifies the type of and key properties for
* the resulting DataObject. * the resulting DataObject.
**/ **/
public DataObject create(OID oid) { public DataObject create(OID oid) {
DataObject result = new DataObjectImpl(oid); DataObject result = new DataObjectImpl(oid);
try { try {
@ -332,7 +326,6 @@ public class Session {
return result; return result;
} }
/** /**
* Retrieves the DataObject specified by <i>oid</i>. If there is * Retrieves the DataObject specified by <i>oid</i>. If there is
* no object of the given type with the given OID, then null is returned. * no object of the given type with the given OID, then null is returned.
@ -344,12 +337,10 @@ public class Session {
* *
* @return A persistent object of the type specified by the oid. * @return A persistent object of the type specified by the oid.
**/ **/
public DataObject retrieve(OID oid) { public DataObject retrieve(OID oid) {
return (DataObject) m_ssn.retrieve(C.pmap(getRoot(), oid)); return (DataObject) m_ssn.retrieve(C.pmap(getRoot(), oid));
} }
/** /**
* Deletes the persistent object of the given type with the given oid. * Deletes the persistent object of the given type with the given oid.
* *
@ -357,7 +348,6 @@ public class Session {
* *
* @return True of an object was deleted, false otherwise. * @return True of an object was deleted, false otherwise.
**/ **/
public boolean delete(OID oid) { public boolean delete(OID oid) {
DataObject dobj = retrieve(oid); DataObject dobj = retrieve(oid);
boolean result = m_ssn.delete(dobj); boolean result = m_ssn.delete(dobj);
@ -366,7 +356,6 @@ public class Session {
return result; return result;
} }
/** /**
* Retrieves a collection of objects of the specified objectType. * Retrieves a collection of objects of the specified objectType.
* This method executes the <code>retrieveAll</code> event defined * This method executes the <code>retrieveAll</code> event defined
@ -377,14 +366,10 @@ public class Session {
* @return A DataCollection of the specified type. * @return A DataCollection of the specified type.
* @see Session#retrieve(String) * @see Session#retrieve(String)
**/ **/
public DataCollection retrieve(ObjectType type) { public DataCollection retrieve(ObjectType type) {
return new DataCollectionImpl return new DataCollectionImpl(this, m_ssn.getDataSet(getRoot().getObjectType(type.getQualifiedName())));
(this, m_ssn.getDataSet(getRoot().getObjectType
(type.getQualifiedName())));
} }
/** /**
* <p>Retrieves a collection of objects of the specified objectType. * <p>Retrieves a collection of objects of the specified objectType.
* This method executes the <code>retrieveAll</code> event defined * This method executes the <code>retrieveAll</code> event defined
@ -426,7 +411,6 @@ public class Session {
* <code>retrieveAll</code> event.. * <code>retrieveAll</code> event..
* @see Session#retrieve(ObjectType) * @see Session#retrieve(ObjectType)
**/ **/
public DataCollection retrieve(String typeName) { public DataCollection retrieve(String typeName) {
ObjectType ot = m_root.getObjectType(typeName); ObjectType ot = m_root.getObjectType(typeName);
if (ot == null) { if (ot == null) {
@ -435,7 +419,6 @@ public class Session {
return retrieve(ot); return retrieve(ot);
} }
/** /**
* <p>Retrieves a persistent query object based on the named query. * <p>Retrieves a persistent query object based on the named query.
* The query must be defined with the specified name in the * The query must be defined with the specified name in the
@ -478,10 +461,8 @@ public class Session {
* @param name The name of the query. * @param name The name of the query.
* @return A new DataQuery object. * @return A new DataQuery object.
**/ **/
public DataQuery retrieveQuery(String name) { public DataQuery retrieveQuery(String name) {
com.redhat.persistence.metadata.ObjectType ot com.redhat.persistence.metadata.ObjectType ot = getRoot().getObjectType(name);
= getRoot().getObjectType(name);
if (ot == null) { if (ot == null) {
throw new PersistenceException("no such query: " + name); throw new PersistenceException("no such query: " + name);
} }
@ -518,10 +499,8 @@ public class Session {
* in the PDL. * in the PDL.
* *
**/ **/
public DataOperation retrieveDataOperation(String name) { public DataOperation retrieveDataOperation(String name) {
com.redhat.persistence.metadata.DataOperation op com.redhat.persistence.metadata.DataOperation op = getRoot().getDataOperation(Path.get(name));
= getRoot().getDataOperation(Path.get(name));
if (op == null) { if (op == null) {
throw new PersistenceException("no such data operation: " + name); throw new PersistenceException("no such data operation: " + name);
} }
@ -548,7 +527,7 @@ public class Session {
} }
void invalidateDataObjects(boolean connectedOnly, boolean error) { void invalidateDataObjects(boolean connectedOnly, boolean error) {
for (Iterator it = m_dataObjects.iterator(); it.hasNext(); ) { for (Iterator it = m_dataObjects.iterator(); it.hasNext();) {
WeakReference ref = (WeakReference) it.next(); WeakReference ref = (WeakReference) it.next();
DataObjectImpl obj = (DataObjectImpl) ref.get(); DataObjectImpl obj = (DataObjectImpl) ref.get();
if (obj != null) { if (obj != null) {
@ -559,10 +538,12 @@ public class Session {
m_dataObjects.clear(); m_dataObjects.clear();
} }
private static class AfterActivate extends EventProcessor { private static class AfterActivate extends EventProcessor {
protected void write(Event ev) { protected void write(Event ev) {
if (!(ev.getObject() instanceof DataObjectImpl)) { return; } if (!(ev.getObject() instanceof DataObjectImpl)) {
return;
}
if (ev instanceof PropertyEvent) { if (ev instanceof PropertyEvent) {
PropertyEvent pe = (PropertyEvent) ev; PropertyEvent pe = (PropertyEvent) ev;
@ -571,58 +552,66 @@ public class Session {
} }
DataObjectImpl doi = (DataObjectImpl) ev.getObject(); DataObjectImpl doi = (DataObjectImpl) ev.getObject();
if (doi.isDeleted()) { return; } if (doi.isDeleted()) {
return;
}
} }
ev.dispatch(new Event.Switch() { ev.dispatch(new Event.Switch() {
public void onCreate(CreateEvent e) { }
public void onDelete(DeleteEvent e) { } public void onCreate(CreateEvent e) {
}
public void onDelete(DeleteEvent e) {
}
public void onSet(com.redhat.persistence.SetEvent e) { public void onSet(com.redhat.persistence.SetEvent e) {
new SetEvent((DataObjectImpl) e.getObject(), new SetEvent((DataObjectImpl) e.getObject(),
e.getProperty().getName(), e.getProperty().getName(),
e.getPreviousValue(), e.getPreviousValue(),
e.getArgument()).fire(); e.getArgument()).fire();
} }
public void onAdd(com.redhat.persistence.AddEvent e) { public void onAdd(com.redhat.persistence.AddEvent e) {
new AddEvent((DataObjectImpl) e.getObject(), new AddEvent((DataObjectImpl) e.getObject(),
e.getProperty().getName(), e.getProperty().getName(),
(DataObjectImpl) e.getArgument()).fire(); (DataObjectImpl) e.getArgument()).fire();
} }
public void onRemove public void onRemove(com.redhat.persistence.RemoveEvent e) {
(com.redhat.persistence.RemoveEvent e) {
new RemoveEvent((DataObjectImpl) e.getObject(), new RemoveEvent((DataObjectImpl) e.getObject(),
e.getProperty().getName(), e.getProperty().getName(),
(DataObjectImpl) e.getArgument()).fire(); (DataObjectImpl) e.getArgument()).fire();
} }
}); });
} }
protected void flush() { } protected void flush() {
protected void cleanUp(boolean isCommit) { } }
protected void cleanUp(boolean isCommit) {
}
} }
static class FlushEventProcessor extends EventProcessor { static class FlushEventProcessor extends EventProcessor {
final private boolean m_before; final private boolean m_before;
private List m_events = new ArrayList(); private List m_events = new ArrayList();
private List m_toFire = new LinkedList(); private List m_toFire = new LinkedList();
FlushEventProcessor(boolean before) { m_before = before; } FlushEventProcessor(boolean before) {
m_before = before;
}
protected void cleanUp(boolean isCommit) { protected void cleanUp(boolean isCommit) {
if (isCommit && m_events.size() > 0) { if (isCommit && m_events.size() > 0) {
LOG.error("unfired data events: " + m_events); LOG.error("unfired data events: " + m_events);
throw new IllegalStateException throw new IllegalStateException("unfired data events: " + m_events);
("unfired data events: " + m_events);
} }
m_events.clear(); m_events.clear();
if (isCommit && m_toFire.size() > 0) { if (isCommit && m_toFire.size() > 0) {
LOG.error("unfired data events: " + m_toFire); LOG.error("unfired data events: " + m_toFire);
throw new IllegalStateException throw new IllegalStateException("unfired data events: " + m_toFire);
("unfired data events: " + m_toFire);
} }
m_toFire.clear(); m_toFire.clear();
} }
@ -630,7 +619,7 @@ public class Session {
protected void write(Event e) { protected void write(Event e) {
if (e.getObject() instanceof DataObjectImpl) { if (e.getObject() instanceof DataObjectImpl) {
if (e instanceof PropertyEvent if (e instanceof PropertyEvent
&& ((PropertyEvent) e).getProperty().getName().charAt(0) && ((PropertyEvent) e).getProperty().getName().charAt(0)
== '~') { == '~') {
return; return;
} }
@ -671,22 +660,26 @@ public class Session {
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
if (events.size() > 0) { if (events.size() > 0) {
LOG.debug((m_before ? "before flush:" : "after flush: ") LOG.debug((m_before ? "before flush:" : "after flush: ")
+ events); + events);
} }
} }
m_toFire.addAll(events); m_toFire.addAll(events);
for (Iterator it = events.iterator(); it.hasNext(); ) { for (Iterator it = events.iterator(); it.hasNext();) {
DataEvent e = (DataEvent) it.next(); DataEvent e = (DataEvent) it.next();
if (e instanceof BeforeDeleteEvent) { continue; } if (e instanceof BeforeDeleteEvent) {
continue;
}
e.schedule(); e.schedule();
} }
for (Iterator it = events.iterator(); it.hasNext(); ) { for (Iterator it = events.iterator(); it.hasNext();) {
DataEvent e = (DataEvent) it.next(); DataEvent e = (DataEvent) it.next();
if (m_toFire.remove(e)) { if (m_toFire.remove(e)) {
if (e instanceof BeforeDeleteEvent) { continue; } if (e instanceof BeforeDeleteEvent) {
continue;
}
e.fire(); e.fire();
} }
} }
@ -699,16 +692,15 @@ public class Session {
} }
private static class DataObjectAdapter extends Adapter { private static class DataObjectAdapter extends Adapter {
public void setSession
(Object obj, com.redhat.persistence.Session ssn) { public void setSession(Object obj, com.redhat.persistence.Session ssn) {
DataObjectImpl dobj = (DataObjectImpl) obj; DataObjectImpl dobj = (DataObjectImpl) obj;
dobj.setSession(ssn); dobj.setSession(ssn);
getSessionFromProto(ssn).addDataObject(dobj); getSessionFromProto(ssn).addDataObject(dobj);
} }
public Object getObject public Object getObject(com.redhat.persistence.metadata.ObjectType type,
(com.redhat.persistence.metadata.ObjectType type, PropertyMap props) {
PropertyMap props) {
if (!type.isKeyed()) { if (!type.isKeyed()) {
return props; return props;
} }
@ -717,8 +709,7 @@ public class Session {
if (type.hasProperty("objectType")) { if (type.hasProperty("objectType")) {
Property p = type.getProperty("objectType"); Property p = type.getProperty("objectType");
if (p.getType().getQualifiedName().equals if (p.getType().getQualifiedName().equals("global.String")) {
("global.String")) {
String qname = (String) props.get(p); String qname = (String) props.get(p);
Root root = type.getRoot(); Root root = type.getRoot();
if (qname != null && root.hasObjectType(qname)) { if (qname != null && root.hasObjectType(qname)) {
@ -730,7 +721,7 @@ public class Session {
MetadataRoot old = MetadataRoot.getMetadataRoot(sp.getRoot()); MetadataRoot old = MetadataRoot.getMetadataRoot(sp.getRoot());
OID oid = new OID(old.getObjectType(sp.getQualifiedName())); OID oid = new OID(old.getObjectType(sp.getQualifiedName()));
for (Iterator it = sp.getKeyProperties().iterator(); for (Iterator it = sp.getKeyProperties().iterator();
it.hasNext(); ) { it.hasNext();) {
Property prop = (Property) it.next(); Property prop = (Property) it.next();
oid.set(prop.getName(), props.get(prop)); oid.set(prop.getName(), props.get(prop));
} }
@ -747,29 +738,27 @@ public class Session {
if (dobj.p_pMap == null) { if (dobj.p_pMap == null) {
final PropertyMap pMap = new PropertyMap(getObjectType(obj)); final PropertyMap pMap = new PropertyMap(getObjectType(obj));
final Iterator it = final Iterator it =
dobj.getOID().getProperties().entrySet().iterator(); dobj.getOID().getProperties().entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next(); Map.Entry me = (Map.Entry) it.next();
pMap.put(getObjectType(obj) pMap.put(getObjectType(obj).getProperty((String) me.getKey()), me.getValue());
.getProperty((String) me.getKey()), me.getValue());
} }
dobj.p_pMap = pMap; dobj.p_pMap = pMap;
} }
return dobj.p_pMap; return dobj.p_pMap;
} }
public com.redhat.persistence.metadata.ObjectType public com.redhat.persistence.metadata.ObjectType getObjectType(Object obj) {
getObjectType(Object obj) {
if (obj instanceof PropertyMap) { if (obj instanceof PropertyMap) {
return ((PropertyMap) obj).getObjectType(); return ((PropertyMap) obj).getObjectType();
} }
final DataObjectImpl dobj = (DataObjectImpl) obj; final DataObjectImpl dobj = (DataObjectImpl) obj;
if (dobj.p_objectType== null) { if (dobj.p_objectType == null) {
dobj.p_objectType = dobj.p_objectType =
C.type(this.getRoot(), dobj.getObjectType()); C.type(this.getRoot(), dobj.getObjectType());
} }
return dobj.p_objectType; return dobj.p_objectType;
@ -777,18 +766,28 @@ public class Session {
} }
private class PSession extends com.redhat.persistence.Session { private class PSession extends com.redhat.persistence.Session {
PSession(Root root, Engine engine, QuerySource source) { PSession(Root root, Engine engine, QuerySource source) {
super(root, engine, source); super(root, engine, source);
} }
private Session getOldSession() { return Session.this; }
}
static Session getSessionFromProto(com.redhat.persistence.Session ssn) { private Session getOldSession() {
try { return Session.this;
return ((PSession) ssn).getOldSession();
} catch (ClassCastException cce) {
return null;
} }
} }
static Session getSessionFromProto(com.redhat.persistence.Session ssn) {
// Test for non null ssn
if (ssn != null) {
try {
return ((PSession) ssn).getOldSession();
} catch (ClassCastException cce) {
return null;
}
} else {
// else return null
return null;
}
}
} }