Source Code Cross Referenced for JDOBasePersistenceAspect.java in  » Web-Framework » roma-webwizard » org » romaframework » aspect » persistence » jdo » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » roma webwizard » org.romaframework.aspect.persistence.jdo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.romaframework.aspect.persistence.jdo;
017:
018:        import java.sql.Connection;
019:        import java.util.List;
020:        import java.util.Map;
021:
022:        import javax.jdo.JDOHelper;
023:        import javax.jdo.PersistenceManager;
024:        import javax.jdo.PersistenceManagerFactory;
025:
026:        import org.apache.commons.logging.Log;
027:        import org.apache.commons.logging.LogFactory;
028:        import org.jpox.enhancer.JPOXEnhancer;
029:        import org.romaframework.aspect.persistence.PersistenceAspect;
030:        import org.romaframework.aspect.persistence.PersistenceAspectAbstract;
031:        import org.romaframework.aspect.persistence.PersistenceContextInjector;
032:        import org.romaframework.aspect.persistence.PersistenceException;
033:        import org.romaframework.aspect.persistence.QueryByExample;
034:        import org.romaframework.aspect.persistence.QueryByFilter;
035:        import org.romaframework.aspect.persistence.QueryByText;
036:        import org.romaframework.aspect.persistence.jdbc.JDBCDatasource;
037:        import org.romaframework.core.flow.ObjectContext;
038:
039:        /**
040:         * Abstract class to handle persistence using JPOX tool and JDO 2 technology. JDO 2.0: http://jcp.org/en/jsr/detail?id=243 <br/>
041:         * JPOX: http://www.jpox.org
042:         * 
043:         * @author Luca Garulli (luca.garulli@assetdata.it)
044:         */
045:        public abstract class JDOBasePersistenceAspect extends
046:                PersistenceAspectAbstract implements  JDBCDatasource {
047:
048:            protected byte strategy;
049:            protected static PersistenceManagerFactory factory;
050:            protected static final PersistenceContextInjector injector = new PersistenceContextInjector();
051:            private static Log log = LogFactory
052:                    .getLog(JDOBasePersistenceAspect.class);
053:            private OIDManager oidManager;
054:
055:            public JDOBasePersistenceAspect(OIDManager iOIDManager) {
056:                oidManager = iOIDManager;
057:            }
058:
059:            public JDOBasePersistenceAspect(Map<String, String> iConfiguration) {
060:                configuration = iConfiguration;
061:                loadFactoryByConfiguration();
062:            }
063:
064:            public JDOBasePersistenceAspect(JDOBasePersistenceAspect iSource) {
065:                if (factory == null) {
066:                    synchronized (this ) {
067:                        if (factory == null) {
068:                            factory = iSource.getPersistenceManagerFactory();
069:                            oidManager = iSource.getOidManager();
070:                        }
071:                    }
072:                }
073:                if (oidManager == null)
074:                    oidManager = iSource.getOidManager();
075:                init();
076:            }
077:
078:            protected abstract void init();
079:
080:            protected abstract void beginOperation(PersistenceManager iManager);
081:
082:            protected abstract void endOperation(PersistenceManager iManager);
083:
084:            protected abstract void closeOperation(PersistenceManager iManager);
085:
086:            public abstract PersistenceManager getPersistenceManager();
087:
088:            public void enhanceAllPersistentClasses() {
089:                enhancePersistentClasses(ObjectContext.getInstance()
090:                        .getComponent(JDOMetadataResourceResolver.class)
091:                        .getMetadatas());
092:            }
093:
094:            public void enhancePersistentClasses(String[] jdoMetadataFiles) {
095:                try {
096:                    JPOXEnhancer.main(jdoMetadataFiles);
097:                } catch (Exception e) {
098:                    log
099:                            .error(
100:                                    "[JDOBasePersistenceAspect.enhancePersistentClasses] Cannot enhance persistent classes",
101:                                    e);
102:                    throw new PersistenceException(
103:                            "Cannot enhance persistent classes", e);
104:                }
105:            }
106:
107:            @Override
108:            public void setConfiguration(Map<String, String> config) {
109:                configuration = config;
110:                loadFactoryByConfiguration();
111:            }
112:
113:            public String getOID(Object iObject) throws PersistenceException {
114:                Object oid = JDOHelper.getObjectId(iObject);
115:
116:                if (oid == null)
117:                    return null;
118:                return oid.toString();
119:            }
120:
121:            public <T> T loadObject(T iObject, String iMode)
122:                    throws PersistenceException {
123:                return loadObject(iObject, iMode, strategy);
124:            }
125:
126:            public <T> T loadObject(T iObject, String iMode, byte iStrategy)
127:                    throws PersistenceException {
128:                if (log.isDebugEnabled())
129:                    log.debug("[JDOPersistenceAspect.loadObject] obj: "
130:                            + iObject + " mode: " + iMode + ", strategy: "
131:                            + iStrategy);
132:
133:                if (iStrategy == PersistenceAspect.STRATEGY_STANDARD
134:                        && !JDOHelper.isDetached(iObject))
135:                    return iObject;
136:
137:                PersistenceManager manager = null;
138:                try {
139:                    manager = getPersistenceManager();
140:
141:                    if (iMode != null)
142:                        // SET FETCH GROUP POLICY
143:                        manager.getFetchPlan().addGroup(iMode);
144:
145:                    beginOperation(manager);
146:
147:                    T freshObj = iObject;
148:
149:                    if (iStrategy == PersistenceAspect.STRATEGY_DETACHING
150:                            || iStrategy == PersistenceAspect.STRATEGY_STANDARD) {
151:                        Object oid = JDOHelper.getObjectId(iObject);
152:                        if (oid == null)
153:                            // NO OID, MAYBE EMBEDDED INSTANCE: JUST RETURN IT
154:                            return null;
155:
156:                        freshObj = (T) manager.getObjectById(oid, true);
157:                    }
158:
159:                    return (T) JDOPersistenceHelper.retrieveObject(manager,
160:                            iMode, iStrategy, freshObj);
161:                } catch (Throwable e) {
162:                    throw new PersistenceException(
163:                            "$PersistenceAspect.loadObject.error", e);
164:                } finally {
165:                    endOperation(manager);
166:                }
167:            }
168:
169:            public <T> T loadObjectByOID(String iOID, String iMode)
170:                    throws PersistenceException {
171:                return (T) loadObjectByOID(iOID, iMode, strategy);
172:            }
173:
174:            public <T> T loadObjectByOID(String iOID, String iMode,
175:                    byte iStrategy) throws PersistenceException {
176:                if (log.isDebugEnabled())
177:                    log.debug("[JDOPersistenceAspect.loadObjectByOID] oid: "
178:                            + iOID + " mode: " + iMode + ", strategy: "
179:                            + iStrategy);
180:
181:                PersistenceManager manager = null;
182:                try {
183:                    manager = getPersistenceManager();
184:
185:                    if (iMode != null)
186:                        // SET FETCH GROUP POLICY
187:                        manager.getFetchPlan().addGroup(iMode);
188:
189:                    beginOperation(manager);
190:
191:                    T freshObj = null;
192:
193:                    Object oid = oidManager.getOID(manager, iOID);
194:
195:                    if (iStrategy == PersistenceAspect.STRATEGY_DETACHING
196:                            || iStrategy == PersistenceAspect.STRATEGY_STANDARD) {
197:                        freshObj = (T) manager.getObjectById(oid, true);
198:                    }
199:
200:                    return (T) JDOPersistenceHelper.retrieveObject(manager,
201:                            iMode, iStrategy, freshObj);
202:                } catch (Throwable e) {
203:                    throw new PersistenceException(
204:                            "$PersistenceAspect.loadObjectByOID.error", e);
205:                } finally {
206:                    endOperation(manager);
207:                }
208:            }
209:
210:            public <T> T createObject(Object iObject)
211:                    throws PersistenceException {
212:                if (log.isDebugEnabled())
213:                    log.debug("[JDOPersistenceAspect.createObject] " + iObject);
214:
215:                PersistenceManager manager = null;
216:                try {
217:                    manager = getPersistenceManager();
218:                    beginOperation(manager);
219:                    iObject = manager.makePersistent(iObject);
220:
221:                    return (T) JDOPersistenceHelper.retrieveObject(manager,
222:                            null, getStrategy(), iObject);
223:                } catch (Throwable e) {
224:                    log.error("[JDOPersistenceAspect.createObject]", e);
225:                    throw new PersistenceException(
226:                            "$PersistenceAspect.createObject.error", e);
227:                } finally {
228:                    endOperation(manager);
229:                }
230:            }
231:
232:            public <T> T updateObject(Object iObject)
233:                    throws PersistenceException {
234:                PersistenceManager manager = null;
235:                try {
236:                    manager = getPersistenceManager();
237:                    beginOperation(manager);
238:
239:                    if (log.isDebugEnabled())
240:                        log.debug("[JDOPersistenceAspect.updateObject] "
241:                                + iObject);
242:
243:                    iObject = manager.makePersistent(iObject);
244:
245:                    return (T) JDOPersistenceHelper.retrieveObject(manager,
246:                            null, getStrategy(), iObject);
247:                } catch (Throwable e) {
248:                    log.error("[JDOPersistenceAspect.updateObject]", e);
249:                    throw new PersistenceException(
250:                            "$PersistenceAspect.updateObject.error", e);
251:                } finally {
252:                    endOperation(manager);
253:                }
254:            }
255:
256:            public void updateObjects(Object[] iObjects)
257:                    throws PersistenceException {
258:                PersistenceManager manager = null;
259:                try {
260:                    manager = getPersistenceManager();
261:                    beginOperation(manager);
262:
263:                    Object currObject;
264:                    for (int i = 0; i < iObjects.length; ++i) {
265:                        currObject = iObjects[i];
266:
267:                        if (log.isDebugEnabled())
268:                            log.debug("[JDOPersistenceAspect.updateObjects] "
269:                                    + currObject);
270:
271:                        manager.makePersistent(currObject);
272:                    }
273:                } catch (Throwable e) {
274:                    log.error("[JDOPersistenceAspect.updateObjects]", e);
275:                    throw new PersistenceException(
276:                            "$PersistenceAspect.updateObject.error", e);
277:                } finally {
278:                    endOperation(manager);
279:                }
280:            }
281:
282:            public void deleteObjects(Object[] iObjects)
283:                    throws PersistenceException {
284:                PersistenceManager manager = null;
285:                try {
286:                    manager = getPersistenceManager();
287:
288:                    beginOperation(manager);
289:
290:                    Object currObject;
291:                    for (int i = 0; i < iObjects.length; ++i) {
292:                        currObject = iObjects[i];
293:
294:                        if (log.isDebugEnabled())
295:                            log.debug("[JDOPersistenceAspect.deleteObjects] "
296:                                    + currObject);
297:
298:                        if (JDOHelper.isDetached(currObject)) {
299:                            Object pObj = manager.makePersistent(currObject);
300:                            manager.deletePersistent(pObj);
301:                        } else {
302:                            manager.deletePersistent(currObject);
303:                        }
304:                    }
305:                } catch (Throwable e) {
306:                    log.error("[JDOPersistenceAspect.deleteObjects]", e);
307:                    throw new PersistenceException(
308:                            "$PersistenceAspect.deleteObject.error", e);
309:                } finally {
310:                    endOperation(manager);
311:                }
312:            }
313:
314:            public void deleteObject(Object iObject)
315:                    throws PersistenceException {
316:                PersistenceManager manager = null;
317:                try {
318:                    manager = getPersistenceManager();
319:
320:                    beginOperation(manager);
321:
322:                    if (log.isDebugEnabled())
323:                        log.debug("[JDOPersistenceAspect.deleteObject] "
324:                                + iObject);
325:
326:                    if (JDOHelper.isDetached(iObject)) {
327:                        Object pObj = manager.makePersistent(iObject);
328:                        manager.deletePersistent(pObj);
329:                    } else {
330:                        manager.deletePersistent(iObject);
331:                    }
332:                } catch (Throwable e) {
333:                    log.error("[JDOPersistenceAspect.deleteObject]", e);
334:                    throw new PersistenceException(
335:                            "$PersistenceAspect.deleteObject.error", e);
336:                } finally {
337:                    endOperation(manager);
338:                }
339:            }
340:
341:            public List query(org.romaframework.aspect.persistence.Query iQuery)
342:                    throws PersistenceException {
343:                PersistenceManager manager = null;
344:                List result = null;
345:                try {
346:                    manager = getPersistenceManager();
347:
348:                    beginOperation(manager);
349:
350:                    if (iQuery.getStrategy() == STRATEGY_DEFAULT)
351:                        // ASSIGN PERSISTENCE ASPECT STRATEGY
352:                        iQuery.setStrategy(strategy);
353:
354:                    if (iQuery instanceof  QueryByExample) {
355:                        QueryByExample queryInput = (QueryByExample) iQuery;
356:                        result = JDOPersistenceHelper.queryByExample(manager,
357:                                queryInput);
358:                    } else if (iQuery instanceof  QueryByFilter) {
359:                        QueryByFilter queryInput = (QueryByFilter) iQuery;
360:                        result = JDOPersistenceHelper.queryByFilter(manager,
361:                                queryInput);
362:                    } else if (iQuery instanceof  QueryByText) {
363:                        QueryByText queryInput = (QueryByText) iQuery;
364:                        result = JDOPersistenceHelper.queryByText(manager,
365:                                queryInput);
366:                    }
367:
368:                    if (result != null)
369:                        iQuery.setResult(result);
370:
371:                } catch (Throwable e) {
372:                    log.error("[JDOPersistenceAspect.query]", e);
373:                    throw new PersistenceException(
374:                            "$PersistenceAspect.query.error", e);
375:                } finally {
376:                    endOperation(manager);
377:                }
378:                return result;
379:            }
380:
381:            public byte getStrategy() {
382:                return strategy;
383:            }
384:
385:            public void setStrategy(byte iStrategy) {
386:                this .strategy = iStrategy;
387:            }
388:
389:            public String aspectName() {
390:                return "persistence";
391:            }
392:
393:            @Override
394:            public void shutdown() {
395:                factory.close();
396:            }
397:
398:            public Connection getConnection() {
399:                return (Connection) JDOPersistenceHelper.getPersistenceManager(
400:                        factory).getDataStoreConnection().getNativeConnection();
401:            }
402:
403:            public PersistenceManagerFactory getPersistenceManagerFactory() {
404:                return factory;
405:            }
406:
407:            private void loadFactoryByConfiguration() {
408:                if (factory == null) {
409:                    synchronized (this ) {
410:                        if (factory == null) {
411:                            factory = JDOHelper
412:                                    .getPersistenceManagerFactory(configuration);
413:                        }
414:                    }
415:                }
416:                init();
417:            }
418:
419:            public OIDManager getOidManager() {
420:                return oidManager;
421:            }
422:
423:            public void setOidManager(OIDManager oidManager) {
424:                this .oidManager = oidManager;
425:            }
426:
427:            public boolean isActive() {
428:                return !getPersistenceManager().isClosed();
429:            }
430:
431:            public static PersistenceContextInjector getInjector() {
432:                return injector;
433:            }
434:
435:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.