You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2004/08/14 22:10:08 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/dirty UnitTest.java
brianm 2004/08/14 13:10:08
Modified: src/java/org/apache/ojb/dirty DirtyBrokerFactory.java
UnitAdaptor.java
Added: src/java/org/apache/ojb/dirty DirtyPersistenceBroker.java
src/test/org/apache/ojb/dirty UnitTest.java
Removed: src/java/org/apache/ojb/broker/dirty
DirtyPersistenceBrokerFactory.java Entry.java
LifeCycleListener.java ManagedEntry.java
PersistenceAware.java PersistenceAwareEntry.java
StateListener.java StateReporter.java
StateReporterImpl.java Tracker.java
src/test/org/apache/ojb/broker PersistenceAwarePerson.java
src/test/org/apache/ojb/broker/dirty DirtyTest.java
Log:
Add a temporary (until final API is decided) way to expose the Unit from grafolia on a PB instance, add unit test demonstrating lazy graph insertion.
Revision Changes Path
1.2 +77 -6 db-ojb/src/java/org/apache/ojb/dirty/DirtyBrokerFactory.java
Index: DirtyBrokerFactory.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/DirtyBrokerFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DirtyBrokerFactory.java 9 Aug 2004 23:38:31 -0000 1.1
+++ DirtyBrokerFactory.java 14 Aug 2004 20:10:07 -0000 1.2
@@ -15,30 +15,40 @@
package org.apache.ojb.dirty;
import org.apache.commons.grafolia.DefaultUnit;
+import org.apache.ojb.broker.OJB;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.PersistenceBrokerFactory;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
public class DirtyBrokerFactory
{
private final PBKey key;
+ private final OJB ojb;
public DirtyBrokerFactory()
{
- this(PersistenceBrokerFactory.getDefaultKey());
+ ojb = new OJB();
+ PersistenceBroker pb = ojb.lookupBroker();
+ key = pb.getPBKey();
+ pb.close();
}
public DirtyBrokerFactory(final PBKey key)
{
+ ojb = new OJB();
this.key = key;
}
public DirtyBrokerFactory(final String db, final String login, final String pass)
{
+ ojb = new OJB();
PersistenceBroker broker = null;
try
{
- broker = PersistenceBrokerFactory.createPersistenceBroker(db, login, pass);
+ broker = ojb.lookupBroker(db, login, pass);
key = broker.getPBKey();
}
finally
@@ -47,12 +57,73 @@
}
}
- public PersistenceBroker getBroker()
+ public DirtyPersistenceBroker getBroker()
{
- final PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker(key);
+ final PersistenceBroker broker = ojb.lookupBroker(key);
final UnitAdaptor adaptor = new UnitAdaptor(broker, new DefaultUnit());
broker.addListener(new ObjectRegistrationListener(adaptor));
broker.addListener(new BrokerStateMonitor(adaptor));
- return broker;
+ Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+ new Class[]{DirtyPersistenceBroker.class},
+ new MyInvocationHandler(broker, adaptor));
+ return (DirtyPersistenceBroker) proxy;
+ }
+
+ private static class MyInvocationHandler implements InvocationHandler
+ {
+ private final PersistenceBroker broker;
+ private final UnitAdaptor unit;
+
+ public MyInvocationHandler(final PersistenceBroker broker, final UnitAdaptor unit)
+ {
+ this.broker = broker;
+ this.unit = unit;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ if (method.getDeclaringClass().equals(DirtyPersistenceBroker.class))
+ {
+ return unit.getUnit();
+ }
+ else
+ {
+ return method.invoke(broker, args);
+ }
+ }
}
+// private final PBKey key;
+//
+// public DirtyBrokerFactory()
+// {
+// this(PersistenceBrokerFactory.getDefaultKey());
+// }
+//
+// public DirtyBrokerFactory(final PBKey key)
+// {
+// this.key = key;
+// }
+//
+// public DirtyBrokerFactory(final String db, final String login, final String pass)
+// {
+// PersistenceBroker broker = null;
+// try
+// {
+// broker = PersistenceBrokerFactory.createPersistenceBroker(db, login, pass);
+// key = broker.getPBKey();
+// }
+// finally
+// {
+// if (broker != null) broker.close();
+// }
+// }
+//
+// public PersistenceBroker getBroker()
+// {
+// final PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker(key);
+// final UnitAdaptor adaptor = new UnitAdaptor(broker, new DefaultUnit());
+// broker.addListener(new ObjectRegistrationListener(adaptor));
+// broker.addListener(new BrokerStateMonitor(adaptor));
+// return broker;
+// }
}
1.2 +5 -0 db-ojb/src/java/org/apache/ojb/dirty/UnitAdaptor.java
Index: UnitAdaptor.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/UnitAdaptor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnitAdaptor.java 9 Aug 2004 23:38:31 -0000 1.1
+++ UnitAdaptor.java 14 Aug 2004 20:10:07 -0000 1.2
@@ -80,4 +80,9 @@
{
changes.add(new Insert(inserted));
}
+
+ public Unit getUnit()
+ {
+ return unit;
+ }
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/DirtyPersistenceBroker.java
Index: DirtyPersistenceBroker.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ojb.dirty;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.commons.grafolia.Unit;
public interface DirtyPersistenceBroker extends PersistenceBroker
{
public Unit getUnit();
}
1.1 db-ojb/src/test/org/apache/ojb/dirty/UnitTest.java
Index: UnitTest.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ojb.dirty;
import junit.framework.TestCase;
import org.apache.commons.grafolia.Unit;
import org.apache.ojb.broker.query.QueryFactory;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.PersistenceBroker;
import java.util.Collection;
public class UnitTest extends TestCase
{
private DirtyBrokerFactory factory = new DirtyBrokerFactory();
public void setUp() throws Exception
{
PersistenceBroker broker = factory.getBroker();
broker.beginTransaction();
Criteria all = null;
broker.deleteByQuery(QueryFactory.newQuery(AddressDesc.class, all));
broker.deleteByQuery(QueryFactory.newQuery(Person.class, all));
broker.deleteByQuery(QueryFactory.newQuery(Address.class, all));
broker.commitTransaction();
}
public void testGetUnit()
{
DirtyPersistenceBroker broker = factory.getBroker();
broker.beginTransaction();
Unit unit = broker.getUnit();
assertNotNull(unit);
broker.commitTransaction();
}
public void testLazyGraphInsert()
{
DirtyPersistenceBroker broker = factory.getBroker();
broker.beginTransaction();
Unit unit = broker.getUnit();
Person person = new Person("Chris", "Double");
person.setMainAddress(new Address("USA", "New York", "5th Avenue"));
person.addOtherAddress("vacation", new Address("USA", "Lake George", "Reed St"));
// this registers person as root of graph with the unit
// and will be persisted when the unit flushes
unit.insert(person);
broker.commitTransaction();
broker.clearCache();
Collection addresses = broker.getCollectionByQuery(QueryFactory.newQuery(Address.class,
(Criteria)null));
assertEquals(2, addresses.size());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org