Source Code Cross Referenced for BeanHandler.java in  » Net » Coadunation_1.0.1 » com » rift » coad » lib » bean » 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 » Net » Coadunation_1.0.1 » com.rift.coad.lib.bean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CoadunationLib: The coaduntion implementation library.
003:         * Copyright (C) 2006  Rift IT Contracting
004:         *
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
018:         *
019:         * BeanHandler.java
020:         *
021:         * The handler responsible for pre-processing the requests made on the
022:         * coadunation beans.
023:         */
024:
025:        package com.rift.coad.lib.bean;
026:
027:        // java core import
028:        import java.lang.ClassLoader;
029:        import java.lang.reflect.InvocationHandler;
030:        import java.lang.reflect.Method;
031:        import java.util.Date;
032:        import java.util.regex.Matcher;
033:        import java.util.regex.Pattern;
034:        import java.lang.reflect.Proxy;
035:        import java.lang.reflect.InvocationTargetException;
036:        import javax.naming.Context;
037:        import javax.naming.InitialContext;
038:        import javax.transaction.Status;
039:        import javax.transaction.UserTransaction;
040:
041:        // logging import
042:        import org.apache.log4j.Logger;
043:
044:        // coadunation imports
045:        import com.rift.coad.lib.Resource;
046:        import com.rift.coad.lib.ResourceIndex;
047:        import com.rift.coad.lib.ResourceReleasedException;
048:        import com.rift.coad.lib.cache.CacheRegistry;
049:        import com.rift.coad.lib.cache.CacheEntry;
050:        import com.rift.coad.lib.cache.KeySyncCache;
051:        import com.rift.coad.lib.cache.KeySyncCacheManager;
052:        import com.rift.coad.lib.common.ClassUtil;
053:        import com.rift.coad.lib.common.RandomGuid;
054:        import com.rift.coad.lib.deployment.BeanInfo;
055:        import com.rift.coad.lib.security.Validator;
056:        import com.rift.coad.lib.security.ThreadsPermissionContainer;
057:        import com.rift.coad.lib.audit.AuditTrail;
058:        import org.objectweb.carol.jndi.enc.java.javaURLContextFactory;
059:
060:        /**
061:         * The handler responsible for pre-processing the requests made on the
062:         * coadunation beans.
063:         *
064:         * @author Brett Chaldecott
065:         */
066:        public class BeanHandler implements  InvocationHandler, CacheEntry {
067:
068:            // the class log variable
069:            protected Logger log = Logger
070:                    .getLogger(BeanHandler.class.getName());
071:
072:            // class member variables
073:            private String id = null;
074:            private BeanInfo beanInfo = null;
075:            private String role = null;
076:            private Object subObject = null;
077:            private ThreadsPermissionContainer permissions = null;
078:            private ClassLoader classLoader = null;
079:            private Date touchTime = null;
080:            private boolean released = false;
081:            private BeanCache beanCacheRef = null;
082:            private TransactionBeanCache transactionBeanCacheRef = null;
083:            private ProxyCache proxyCacheRef = null;
084:            private TransactionProxyCache transactionProxyCacheRef = null;
085:            private KeySyncCache keySyncCache = null;
086:            private Context context = null;
087:            private UserTransaction ut = null;
088:            private AuditTrail auditTrail = null;
089:
090:            /** Creates a new instance of BeanHandler */
091:            public BeanHandler(BeanInfo beanInfo, Object subObject,
092:                    String role, ThreadsPermissionContainer permissions,
093:                    ClassLoader classLoader) throws BeanException {
094:                try {
095:                    touch();
096:                    id = RandomGuid.getInstance().getGuid();
097:                    this .beanInfo = beanInfo;
098:                    this .subObject = subObject;
099:                    this .role = role;
100:                    this .permissions = permissions;
101:                    this .classLoader = classLoader;
102:                    context = new InitialContext();
103:                    ut = (UserTransaction) context
104:                            .lookup("java:comp/UserTransaction");
105:                    auditTrail = AuditTrail.getAudit(subObject.getClass());
106:                } catch (Exception ex) {
107:                    throw new BeanException(
108:                            "Failed to instanciate the handler for ["
109:                                    + subObject.getClass().getName()
110:                                    + "] because : " + ex.getMessage(), ex);
111:                }
112:            }
113:
114:            /**
115:             * The invocation handler.
116:             */
117:            public Object invoke(Object proxy, Method method, Object[] args)
118:                    throws Throwable {
119:                if (log.isDebugEnabled()) {
120:                    log.debug("Calling method [" + method.toString() + "]");
121:                }
122:
123:                // check if this object has been released
124:                if (isReleased()) {
125:                    throw new ResourceReleasedException(
126:                            "This beans resources have been released.");
127:                }
128:
129:                boolean validated = false;
130:                touch();
131:                boolean success = true;
132:                try {
133:                    Validator.validate(this .getClass(), role);
134:                    validated = true;
135:                    permissions.pushRole(role);
136:
137:                    // retrieve the method information
138:                    Method subMethod = subObject.getClass().getMethod(
139:                            method.getName(), method.getParameterTypes());
140:
141:                    // create patterns
142:                    Pattern addPattern = Pattern
143:                            .compile(BeanPattern.ADD_PATTERN);
144:                    Pattern findPattern = Pattern
145:                            .compile(BeanPattern.FIND_PATTERN);
146:                    Pattern removePattern = Pattern
147:                            .compile(BeanPattern.REMOVE_PATTERN);
148:
149:                    // check for bean pattern methods
150:                    if (beanInfo.getCacheResults()
151:                            && !beanInfo.getTransaction()
152:                            && addPattern.matcher(method.getName()).find()
153:                            && subMethod.getReturnType().isInterface()
154:                            && !ClassUtil.testForParent(subMethod
155:                                    .getReturnType(),
156:                                    java.io.Serializable.class)) {
157:                        return addMethod(method, args);
158:                    } else if (beanInfo.getCacheResults()
159:                            && beanInfo.getTransaction()
160:                            && addPattern.matcher(method.getName()).find()
161:                            && subMethod.getReturnType().isInterface()
162:                            && !ClassUtil.testForParent(subMethod
163:                                    .getReturnType(),
164:                                    java.io.Serializable.class)) {
165:                        return transactionAddMethod(method, args);
166:                    } else if (beanInfo.getCacheResults()
167:                            && !beanInfo.getTransaction()
168:                            && findPattern.matcher(method.getName()).find()
169:                            && subMethod.getReturnType().isInterface()
170:                            && !ClassUtil.testForParent(subMethod
171:                                    .getReturnType(),
172:                                    java.io.Serializable.class)
173:                            && args.length == 1) {
174:                        return findMethod(method, args);
175:                    } else if (beanInfo.getCacheResults()
176:                            && beanInfo.getTransaction()
177:                            && findPattern.matcher(method.getName()).find()
178:                            && subMethod.getReturnType().isInterface()
179:                            && !ClassUtil.testForParent(subMethod
180:                                    .getReturnType(),
181:                                    java.io.Serializable.class)
182:                            && args.length == 1) {
183:                        return transactionFindMethod(method, args);
184:                    } else if (beanInfo.getCacheResults()
185:                            && !beanInfo.getTransaction()
186:                            && removePattern.matcher(method.getName()).find()
187:                            && !ClassUtil.testForParent(subMethod
188:                                    .getReturnType(),
189:                                    java.io.Serializable.class)
190:                            && args.length == 1) {
191:                        return removeMethod(method, args);
192:                    } else if (beanInfo.getCacheResults()
193:                            && beanInfo.getTransaction()
194:                            && removePattern.matcher(method.getName()).find()
195:                            && !ClassUtil.testForParent(subMethod
196:                                    .getReturnType(),
197:                                    java.io.Serializable.class)
198:                            && args.length == 1) {
199:                        return transactionRemoveMethod(method, args);
200:                    }
201:
202:                    // make the call
203:                    boolean ownTransaction = false;
204:                    Object result = null;
205:                    try {
206:                        if (beanInfo.getTransaction()) {
207:                            try {
208:                                if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
209:                                    ut.begin();
210:                                    ownTransaction = true;
211:                                }
212:                            } catch (Exception ex) {
213:                                log.error("Failed to start the transacton : "
214:                                        + ex.getMessage(), ex);
215:                                throw new BeanException(
216:                                        "Failed to start the transacton : "
217:                                                + ex.getMessage(), ex);
218:                            }
219:                        }
220:                        result = subMethod.invoke(subObject, args);
221:                        // deal with none bean pattern method caching
222:                        if (subMethod.getReturnType().isInterface()
223:                                && !ClassUtil.testForParent(subMethod
224:                                        .getReturnType(),
225:                                        java.io.Serializable.class)) {
226:                            try {
227:                                BeanHandler handler = new BeanHandler(beanInfo,
228:                                        result, beanInfo.getRole(),
229:                                        permissions, classLoader);
230:                                Object newProxy = (Object) Proxy
231:                                        .newProxyInstance(classLoader, result
232:                                                .getClass().getInterfaces(),
233:                                                handler);
234:                                ProxyCache proxyCache = getProxyCache();
235:                                proxyCache.addCacheEntry(beanInfo
236:                                        .getCacheTimeout(), newProxy, handler);
237:                                if (ownTransaction) {
238:                                    ut.commit();
239:                                }
240:                                return newProxy;
241:                            } catch (Exception ex) {
242:                                log.error(
243:                                        "Failed to create the proxy return object : "
244:                                                + ex.getMessage(), ex);
245:                                throw new BeanException(
246:                                        "Failed to create the proxy return object : "
247:                                                + ex.getMessage(), ex);
248:                            }
249:                        }
250:                        if (ownTransaction) {
251:                            ut.commit();
252:                        }
253:                        // return the result if not an interface
254:                        return result;
255:                    } catch (Throwable ex) {
256:                        if (ownTransaction) {
257:                            try {
258:                                if (ut.getStatus() == Status.STATUS_ACTIVE) {
259:                                    ut.rollback();
260:                                }
261:                            } catch (Exception ex2) {
262:                                log.error("Failed to rollback the changes : "
263:                                        + ex2.getMessage(), ex2);
264:                            }
265:                        }
266:                        throw ex;
267:                    }
268:                } catch (InvocationTargetException ex) {
269:                    log.error("An exception was thrown by the sub object : "
270:                            + ex.getMessage(), ex);
271:                    auditTrail.logEvent(method.getName(), ex
272:                            .getTargetException());
273:                    success = false;
274:                    throw ex.getTargetException();
275:                } catch (Throwable ex) {
276:                    log.error("An exception was thrown by the sub object : "
277:                            + ex.getMessage(), ex);
278:                    auditTrail.logEvent(method.getName(), ex);
279:                    success = false;
280:                    throw ex;
281:                } finally {
282:                    if (success) {
283:                        auditTrail.logEvent(method.getName());
284:                    }
285:                    if (validated == true) {
286:                        permissions.popRole(role);
287:                    }
288:                }
289:            }
290:
291:            /**
292:             * This method will return true if the date is older than the given expiry
293:             * date.
294:             *
295:             * @return TRUE if expired FALSE if not.
296:             * @param expiryDate The expiry date to perform the check with.
297:             */
298:            public boolean isExpired(Date expiryDate) {
299:                return touchTime.getTime() < expiryDate.getTime();
300:            }
301:
302:            /**
303:             * This method is called by the cache when releasing this object.
304:             */
305:            public synchronized void cacheRelease() {
306:                if (subObject instanceof  Resource) {
307:                    ((Resource) subObject).releaseResource();
308:                }
309:                released = true;
310:            }
311:
312:            /**
313:             * This method is called to update the touch time of the base object.
314:             */
315:            public synchronized void touch() {
316:                touchTime = new Date();
317:            }
318:
319:            /**
320:             * This method returns the id of this bean handler
321:             *
322:             * @return The string containing the id of this handler
323:             */
324:            protected String getId() {
325:                return id;
326:            }
327:
328:            /**
329:             * This method returns the hash code of this object.
330:             */
331:            public int hashCode() {
332:                return id.hashCode();
333:            }
334:
335:            /**
336:             * This method determines if the bean handlers are the same.
337:             *
338:             * @return TRUE if the same, FALSE if not.
339:             * @param rhs The right hand side.
340:             */
341:            public boolean equals(Object rhs) {
342:                if (!(rhs instanceof  BeanHandler)) {
343:                    return false;
344:                }
345:                return id.equals(((BeanHandler) rhs).getId());
346:            }
347:
348:            /**
349:             * This method checks the status of the released flag.
350:             *
351:             * @return TRUE if released, FALSE if not.
352:             */
353:            private synchronized boolean isReleased() {
354:                return released;
355:            }
356:
357:            /**
358:             * This method will deal with the add call.
359:             *
360:             * @return The results of the add call.
361:             * @param subMethod The method to invoke the call on.
362:             * @param args The arguments to make the call with.
363:             * @exception Throwable
364:             */
365:            private Object addMethod(Method subMethod, Object[] args)
366:                    throws Throwable {
367:
368:                // make the call
369:                Object result = subMethod.invoke(subObject, args);
370:                try {
371:                    BeanHandler handler = new BeanHandler(beanInfo, result,
372:                            beanInfo.getRole(), permissions, classLoader);
373:                    Object proxy = (Object) Proxy.newProxyInstance(classLoader,
374:                            result.getClass().getInterfaces(), handler);
375:                    if (result instanceof  ResourceIndex) {
376:                        BeanCache beanCache = getBeanCache();
377:                        beanCache.addCacheEntry(beanInfo.getCacheTimeout(),
378:                                ((ResourceIndex) result).getPrimaryKey(),
379:                                result, proxy, handler);
380:                    } else {
381:                        ProxyCache proxyCache = getProxyCache();
382:                        proxyCache.addCacheEntry(beanInfo.getCacheTimeout(),
383:                                proxy, handler);
384:                    }
385:                    return proxy;
386:                } catch (Exception ex) {
387:                    log.error("Failed to create the proxy return object : "
388:                            + ex.getMessage(), ex);
389:                    throw new BeanException(
390:                            "Failed to create the proxy return object : "
391:                                    + ex.getMessage(), ex);
392:                }
393:            }
394:
395:            /**
396:             * This method will deal with the add within a transaction call.
397:             *
398:             * @return The results of the add call.
399:             * @param subMethod The method to invoke the call on.
400:             * @param args The arguments to make the call with.
401:             * @exception Throwable
402:             */
403:            private Object transactionAddMethod(Method subMethod, Object[] args)
404:                    throws Throwable {
405:                boolean ownTransaction = false;
406:                try {
407:                    if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
408:                        ut.begin();
409:                        ownTransaction = true;
410:                    }
411:                } catch (Exception ex) {
412:                    log.error("Failed to start the transacton : "
413:                            + ex.getMessage(), ex);
414:                    throw new BeanException("Failed to start the transacton : "
415:                            + ex.getMessage(), ex);
416:                }
417:
418:                // make the call
419:                Object result = null;
420:                try {
421:                    result = subMethod.invoke(subObject, args);
422:                } catch (Throwable ex) {
423:                    if (ownTransaction) {
424:                        try {
425:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
426:                                ut.rollback();
427:                            }
428:                        } catch (Exception ex2) {
429:                            log.error("Failed to rollback the changes : "
430:                                    + ex2.getMessage(), ex2);
431:                        }
432:                    }
433:                    throw ex;
434:                }
435:                try {
436:                    BeanHandler handler = new BeanHandler(beanInfo, result,
437:                            beanInfo.getRole(), permissions, classLoader);
438:                    Object proxy = (Object) Proxy.newProxyInstance(classLoader,
439:                            result.getClass().getInterfaces(), handler);
440:
441:                    if (result instanceof  ResourceIndex) {
442:
443:                        TransactionBeanCache transactionBeanCache = getTransactionBeanCache();
444:                        System.out.println("Add a new entry to the cache ["
445:                                + beanInfo.getCacheTimeout() + "] ["
446:                                + ((ResourceIndex) result).getPrimaryKey()
447:                                + "]");
448:                        transactionBeanCache.addCacheEntry(beanInfo
449:                                .getCacheTimeout(), ((ResourceIndex) result)
450:                                .getPrimaryKey(), result, proxy, handler);
451:                    } else {
452:                        System.out.println("Add a new entry to the cache ["
453:                                + beanInfo.getCacheTimeout() + "]");
454:                        TransactionProxyCache transactionProxyCache = getTransactionProxyCache();
455:                        transactionProxyCache.addCacheEntry(beanInfo
456:                                .getCacheTimeout(), proxy, handler);
457:                    }
458:                    if (ownTransaction) {
459:                        ut.commit();
460:                    }
461:                    return proxy;
462:                } catch (Throwable ex) {
463:                    log.error("Failed to create the proxy return object : "
464:                            + ex.getMessage(), ex);
465:                    if (ownTransaction) {
466:                        try {
467:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
468:                                ut.rollback();
469:                            }
470:                        } catch (Exception ex2) {
471:                            log.error("Failed to rollback the changes : "
472:                                    + ex2.getMessage(), ex2);
473:                        }
474:                    }
475:                    throw new BeanException(
476:                            "Failed to create the proxy return object : "
477:                                    + ex.getMessage(), ex);
478:                }
479:            }
480:
481:            /**
482:             * This method will deal with a find call.
483:             *
484:             * @return The results of the add call.
485:             * @param subMethod The method to invoke the call on.
486:             * @param args The arguments to make the call with.
487:             * @exception Throwable
488:             */
489:            private Object findMethod(Method subMethod, Object[] args)
490:                    throws Throwable {
491:
492:                // synchronize this method based on the key that is passed in.
493:                Object syncObj = getKeySyncCache().getKeySync(args[0]);
494:                synchronized (syncObj) {
495:                    BeanCache beanCache = null;
496:                    try {
497:                        beanCache = getBeanCache();
498:                        BeanCacheEntry cacheEntry = beanCache.getEntry(args[0]);
499:                        if (cacheEntry != null) {
500:                            if (cacheEntry.getProxy() == null) {
501:                                BeanHandler handler = new BeanHandler(beanInfo,
502:                                        cacheEntry.getWrappedObject(), beanInfo
503:                                                .getRole(), permissions,
504:                                        classLoader);
505:                                cacheEntry.setBeanHandler(handler);
506:                                Object proxy = (Object) Proxy.newProxyInstance(
507:                                        classLoader, cacheEntry
508:                                                .getWrappedObject().getClass()
509:                                                .getInterfaces(), handler);
510:                                cacheEntry.setProxy(proxy);
511:                            }
512:                            return cacheEntry.getProxy();
513:                        }
514:                    } catch (Exception ex) {
515:                        log.error("Failed to check the cache : "
516:                                + ex.getMessage(), ex);
517:                        throw new BeanException("Failed to check the cache : "
518:                                + ex.getMessage(), ex);
519:                    }
520:
521:                    Object result = subMethod.invoke(subObject, args);
522:
523:                    try {
524:                        BeanHandler handler = new BeanHandler(beanInfo, result,
525:                                beanInfo.getRole(), permissions, classLoader);
526:                        Object proxy = (Object) Proxy.newProxyInstance(
527:                                classLoader, result.getClass().getInterfaces(),
528:                                handler);
529:                        beanCache.addCacheEntry(beanInfo.getCacheTimeout(),
530:                                args[0], result, proxy, handler);
531:                        return proxy;
532:                    } catch (Exception ex) {
533:                        log.error("Failed to create the proxy return object : "
534:                                + ex.getMessage(), ex);
535:                        throw new BeanException(
536:                                "Failed to create the proxy return object : "
537:                                        + ex.getMessage(), ex);
538:                    }
539:                }
540:            }
541:
542:            /**
543:             * This method finds a object within a transactional scope.
544:             *
545:             * @return The results of the add call.
546:             * @param subMethod The method to invoke the call on.
547:             * @param args The arguments to make the call with.
548:             * @exception Throwable
549:             */
550:            private Object transactionFindMethod(Method subMethod, Object[] args)
551:                    throws Throwable {
552:                boolean ownTransaction = false;
553:                try {
554:                    if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
555:                        ut.begin();
556:                        ownTransaction = true;
557:                    }
558:                } catch (Exception ex) {
559:                    log.error("Failed to start the transacton : "
560:                            + ex.getMessage(), ex);
561:                    throw new BeanException("Failed to start the transacton : "
562:                            + ex.getMessage(), ex);
563:                }
564:
565:                TransactionBeanCache transactionBeanCache = null;
566:                try {
567:                    transactionBeanCache = getTransactionBeanCache();
568:                    BeanCacheEntry cacheEntry = transactionBeanCache
569:                            .getCacheEntry(args[0]);
570:                    if (cacheEntry != null) {
571:                        if (cacheEntry.getProxy() == null) {
572:                            BeanHandler handler = new BeanHandler(beanInfo,
573:                                    cacheEntry.getWrappedObject(), beanInfo
574:                                            .getRole(), permissions,
575:                                    classLoader);
576:                            cacheEntry.setBeanHandler(handler);
577:                            Object proxy = (Object) Proxy.newProxyInstance(
578:                                    classLoader, cacheEntry.getWrappedObject()
579:                                            .getClass().getInterfaces(),
580:                                    handler);
581:                            cacheEntry.setProxy(proxy);
582:                        }
583:                        if (ownTransaction) {
584:                            ut.commit();
585:                        }
586:                        return cacheEntry.getProxy();
587:                    }
588:                } catch (Throwable ex) {
589:                    log.error("Failed to check the cache : " + ex.getMessage(),
590:                            ex);
591:                    if (ownTransaction) {
592:                        try {
593:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
594:                                ut.rollback();
595:                            }
596:                        } catch (Exception ex2) {
597:                            log.error("Failed to rollback the changes : "
598:                                    + ex2.getMessage(), ex2);
599:                        }
600:                    }
601:                    throw new BeanException("Failed to check the cache : "
602:                            + ex.getMessage(), ex);
603:                }
604:
605:                Object result = null;
606:                try {
607:                    result = subMethod.invoke(subObject, args);
608:                } catch (Throwable ex) {
609:                    if (ownTransaction) {
610:                        try {
611:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
612:                                ut.rollback();
613:                            }
614:                        } catch (Exception ex2) {
615:                            log.error("Failed to rollback the changes : "
616:                                    + ex2.getMessage(), ex2);
617:                        }
618:                    }
619:                    throw ex;
620:                }
621:
622:                try {
623:                    BeanHandler handler = new BeanHandler(beanInfo, result,
624:                            beanInfo.getRole(), permissions, classLoader);
625:                    Object proxy = (Object) Proxy.newProxyInstance(classLoader,
626:                            result.getClass().getInterfaces(), handler);
627:                    transactionBeanCache
628:                            .addCacheEntry(beanInfo.getCacheTimeout(), args[0],
629:                                    result, proxy, handler);
630:                    if (ownTransaction) {
631:                        ut.commit();
632:                    }
633:                    return proxy;
634:                } catch (Throwable ex) {
635:                    log.error("Failed to create the proxy return object : "
636:                            + ex.getMessage(), ex);
637:                    if (ownTransaction) {
638:                        try {
639:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
640:                                ut.rollback();
641:                            }
642:                        } catch (Exception ex2) {
643:                            log.error("Failed to rollback the changes : "
644:                                    + ex2.getMessage(), ex2);
645:                        }
646:                    }
647:                    throw new BeanException(
648:                            "Failed to create the proxy return object : "
649:                                    + ex.getMessage(), ex);
650:                }
651:            }
652:
653:            /**
654:             * This method will deal with a find call.
655:             *
656:             * @return The results of the add call.
657:             * @param subMethod The method to invoke the call on.
658:             * @param args The arguments to make the call with.
659:             * @exception Throwable
660:             */
661:            private Object removeMethod(Method subMethod, Object[] args)
662:                    throws Throwable {
663:                // synchronize this method based on the key that is passed in.
664:                Object syncObj = getKeySyncCache().getKeySync(args[0]);
665:                synchronized (syncObj) {
666:                    Object result = subMethod.invoke(subObject, args);
667:
668:                    try {
669:                        BeanCache beanCache = getBeanCache();
670:                        beanCache.removeCacheEntry(args[0]);
671:                    } catch (Exception ex) {
672:                        log.error("Failed to remove bean cache entry : "
673:                                + ex.getMessage(), ex);
674:                        throw new BeanException(
675:                                "Failed to remove bean cache entry : "
676:                                        + ex.getMessage(), ex);
677:                    }
678:                    return result;
679:                }
680:            }
681:
682:            /**
683:             * This method will deal with a find call.
684:             *
685:             * @return The results of the add call.
686:             * @param subMethod The method to invoke the call on.
687:             * @param args The arguments to make the call with.
688:             * @exception Throwable
689:             */
690:            private Object transactionRemoveMethod(Method subMethod,
691:                    Object[] args) throws Throwable {
692:                boolean ownTransaction = false;
693:                try {
694:                    if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
695:                        ut.begin();
696:                        ownTransaction = true;
697:                    }
698:                } catch (Exception ex) {
699:                    log.error("Failed to start the transacton : "
700:                            + ex.getMessage(), ex);
701:                    throw new BeanException("Failed to start the transacton : "
702:                            + ex.getMessage(), ex);
703:                }
704:                Object result = null;
705:                try {
706:                    result = subMethod.invoke(subObject, args);
707:                } catch (Throwable ex) {
708:                    if (ownTransaction) {
709:                        try {
710:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
711:                                ut.rollback();
712:                            }
713:                        } catch (Exception ex2) {
714:                            log.error("Failed to rollback the changes : "
715:                                    + ex2.getMessage(), ex2);
716:                        }
717:                    }
718:                    throw ex;
719:                }
720:
721:                try {
722:                    TransactionBeanCache transactionBeanCache = getTransactionBeanCache();
723:                    transactionBeanCache.removeCacheEntry(args[0]);
724:                    if (ownTransaction) {
725:                        ut.commit();
726:                    }
727:                } catch (Throwable ex) {
728:                    log.error("Failed to remove bean cache entry : "
729:                            + ex.getMessage(), ex);
730:                    if (ownTransaction) {
731:                        try {
732:                            if (ut.getStatus() == Status.STATUS_ACTIVE) {
733:                                ut.rollback();
734:                            }
735:                        } catch (Exception ex2) {
736:                            log.error("Failed to rollback the changes : "
737:                                    + ex2.getMessage(), ex2);
738:                        }
739:                    }
740:                    throw new BeanException(
741:                            "Failed to remove bean cache entry : "
742:                                    + ex.getMessage(), ex);
743:                }
744:                return result;
745:            }
746:
747:            /**
748:             * This method returns the cached reference to the bean cache object.
749:             *
750:             * @return The reference to the bean cache.
751:             * @exception Throwable
752:             */
753:            private synchronized BeanCache getBeanCache() throws Throwable {
754:                if (beanCacheRef == null) {
755:                    BeanCacheManager manager = (BeanCacheManager) CacheRegistry
756:                            .getInstance().getCache(BeanCacheManager.class);
757:                    beanCacheRef = manager.getBeanCache(subObject);
758:                }
759:                return beanCacheRef;
760:            }
761:
762:            /**
763:             * This method returns the cached reference to the bean cache object.
764:             *
765:             * @return The reference to the bean cache.
766:             * @exception Throwable
767:             */
768:            private synchronized TransactionBeanCache getTransactionBeanCache()
769:                    throws Throwable {
770:                if (transactionBeanCacheRef == null) {
771:                    TransactionBeanCacheManager manager = (TransactionBeanCacheManager) CacheRegistry
772:                            .getInstance().getCache(
773:                                    TransactionBeanCacheManager.class);
774:                    transactionBeanCacheRef = manager.getBeanCache(subObject);
775:                }
776:                return transactionBeanCacheRef;
777:            }
778:
779:            /**
780:             * This method returns the cached reference to the bean cache object.
781:             *
782:             * @return The reference to the bean cache.
783:             * @exception Throwable
784:             */
785:            private synchronized ProxyCache getProxyCache() throws Throwable {
786:                if (proxyCacheRef == null) {
787:                    proxyCacheRef = (ProxyCache) CacheRegistry.getInstance()
788:                            .getCache(ProxyCache.class);
789:                }
790:                return proxyCacheRef;
791:            }
792:
793:            /**
794:             * This method returns the cached reference to the bean cache object.
795:             *
796:             * @return The reference to the bean cache.
797:             * @exception Throwable
798:             */
799:            private synchronized TransactionProxyCache getTransactionProxyCache()
800:                    throws Throwable {
801:                if (transactionProxyCacheRef == null) {
802:                    transactionProxyCacheRef = (TransactionProxyCache) CacheRegistry
803:                            .getInstance()
804:                            .getCache(TransactionProxyCache.class);
805:                }
806:                return transactionProxyCacheRef;
807:            }
808:
809:            /**
810:             * This method returns a reference to the key sync cache object.
811:             *
812:             * @return The reference to the key sync cache object.
813:             * @exception Throwable
814:             */
815:            private synchronized KeySyncCache getKeySyncCache()
816:                    throws Throwable {
817:                try {
818:                    if (keySyncCache == null) {
819:                        KeySyncCacheManager keySyncCacheManager = (KeySyncCacheManager) CacheRegistry
820:                                .getInstance().getCache(
821:                                        KeySyncCacheManager.class);
822:                        keySyncCache = keySyncCacheManager
823:                                .getKeySyncCache(this );
824:                    }
825:                    return keySyncCache;
826:                } catch (Exception ex) {
827:                    log.error(
828:                            "Failed to retrieve the key sync cache object ref : "
829:                                    + ex.getMessage(), ex);
830:                    throw new BeanException(
831:                            "Failed to retrieve the key sync cache object ref : "
832:                                    + ex.getMessage(), ex);
833:                }
834:            }
835:
836:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.