Source Code Cross Referenced for ComputationStandalone.java in  » Aspect-oriented » aspectwerkz-2.0 » eworld » service » 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 » Aspect oriented » aspectwerkz 2.0 » eworld.service 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**************************************************************************************
002:         * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
003:         * http://aspectwerkz.codehaus.org                                                    *
004:         * ---------------------------------------------------------------------------------- *
005:         * The software in this package is published under the terms of the LGPL license      *
006:         * a copy of which has been included with this distribution in the license.txt file.  *
007:         **************************************************************************************/package eworld.service;
008:
009:        import org.codehaus.aspectwerkz.extension.hotswap.EWorldUtil;
010:        import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
011:        import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
012:        import org.codehaus.aspectwerkz.joinpoint.MemberSignature;
013:        import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
014:
015:        import java.util.Map;
016:        import java.util.HashMap;
017:
018:        /**
019:         * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
020:         */
021:        public class ComputationStandalone {
022:
023:            private static final int WEAVING_FREQUENCY = new Integer(System
024:                    .getProperty("weaving.frequency")).intValue();
025:
026:            private static final boolean USE_CACHE = System
027:                    .getProperty("cache").equals("true");
028:
029:            private static final boolean USE_TRACE = System
030:                    .getProperty("trace").equals("true");
031:
032:            private static final String EXPRESSION = "execution(int eworld.service.ComputationStandalone.fib(int))";
033:
034:            private static final String SYSTEM_ID = "eworld/wlw/aop";
035:
036:            private static final String CACHE_POINTCUT = "cache";
037:
038:            private static final String TRACE_POINTCUT = "trace";
039:
040:            private static final String CACHE_ADVICE = "cache";
041:
042:            private static final String TRACE_ADVICE = "trace";
043:
044:            public static int fib(int n) {
045:                if (n < 2) {
046:                    System.err.println(n + ".");
047:                    return 1;
048:                } else {
049:                    System.err.print(n + ",");
050:                    return fib(n - 1) + fib(n - 2);
051:                }
052:            }
053:
054:            private static void weave() {
055:                if (USE_CACHE) {
056:                    System.err.println("weaving in cache support");
057:                    EWorldUtil.activate(SYSTEM_ID, CacheAspect.class.getName(),
058:                            CACHE_ADVICE, EXPRESSION, CACHE_POINTCUT);
059:                }
060:                if (USE_TRACE) {
061:                    System.err.println("weaving in trace support");
062:                    EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(),
063:                            TRACE_ADVICE, EXPRESSION, TRACE_POINTCUT);
064:                }
065:            }
066:
067:            private static void unWeave() {
068:                if (USE_CACHE) {
069:                    System.err.println("un-weaving cache support");
070:                    EWorldUtil.deactivate(SYSTEM_ID, CacheAspect.class
071:                            .getName(), CACHE_ADVICE, CACHE_POINTCUT);
072:                    // flush the cache... as if we have a "onUndeploy callback.."
073:                    System.err.println("** Flushing the cache...");
074:                    CacheAspect.s_cache.clear();
075:                }
076:                if (USE_TRACE) {
077:                    System.err.println("un-weaving trace support");
078:                    EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class
079:                            .getName(), TRACE_ADVICE, TRACE_POINTCUT);
080:                }
081:            }
082:
083:            public static void main(String[] args) {
084:
085:                //        if (args.length != 2) {
086:                //            System.err.println("fib(" + 3 + ") = " + fib(3));
087:                //
088:                //            System.err.println("weaving in trace support");
089:                //            EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE, EXPRESSION,
090:                // TRACE_POINTCUT);
091:                //            EWorldUtil.hotswap("eworld.service");
092:                //            System.err.println("fib(" + 3 + ") = " + fib(3));
093:                //
094:                //            System.err.println("weaving in cache support");
095:                //            EWorldUtil.activate(SYSTEM_ID, CacheAspect.class.getName(), CACHE_ADVICE, EXPRESSION,
096:                // CACHE_POINTCUT);
097:                //            EWorldUtil.hotswap("eworld.service");
098:                //            System.err.println("fib(" + 3 + ") = " + fib(3));
099:                //            System.err.println("fib(" + 3 + ") = " + fib(3));
100:                //
101:                //            System.err.println("un-weaving trace support");
102:                //            EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE,
103:                // TRACE_POINTCUT);
104:                //            EWorldUtil.hotswap("eworld.service");
105:                //            System.err.println("fib(" + 4 + ") = " + fib(4));
106:                //
107:                //            System.err.println("un-weaving cache support");
108:                //            EWorldUtil.deactivate(SYSTEM_ID, CacheAspect.class.getName(), CACHE_ADVICE,
109:                // CACHE_POINTCUT);
110:                //            EWorldUtil.hotswap("eworld.service");
111:                //            System.err.println("fib(" + 3 + ") = " + fib(3));
112:                //
113:                //            System.err.println("weaving in trace support");
114:                //            EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE, EXPRESSION,
115:                // TRACE_POINTCUT);
116:                //            EWorldUtil.hotswap("eworld.service");
117:                //            System.err.println("fib(" + 3 + ") = " + fib(3));
118:                //
119:                //            System.err.println("un-weaving trace support");
120:                //            EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE,
121:                // TRACE_POINTCUT);
122:                //            EWorldUtil.hotswap("eworld.service");
123:                //            System.err.println("fib(" + 4 + ") = " + fib(4));
124:                //
125:                //            System.exit(0);
126:                //            //throw new IllegalArgumentException("number of iterations and sleep time must be
127:                // specified");
128:                //        }
129:                try {
130:                    int iterations = new Integer(args[0]).intValue();
131:                    long sleep = new Long(args[1]).longValue();
132:                    int counter = 0;
133:                    boolean isWeaved = false;
134:                    while (true) {
135:                        System.out.println("TraceAspect weave status = "
136:                                + EWorldUtil.isWeaved(SYSTEM_ID,
137:                                        TraceAspect.class.getName()));
138:                        System.out.println("CacheAspect weave status = "
139:                                + EWorldUtil.isWeaved(SYSTEM_ID,
140:                                        CacheAspect.class.getName()));
141:                        counter++;
142:                        Thread.sleep(sleep);
143:                        System.err.println("fib(" + iterations + ") = "
144:                                + fib(iterations));
145:                        if (USE_CACHE || USE_TRACE) {
146:                            if ((counter %= WEAVING_FREQUENCY) == 0) {
147:                                if (isWeaved) {
148:                                    unWeave();
149:                                    isWeaved = false;
150:                                } else {
151:                                    weave();
152:                                    isWeaved = true;
153:                                }
154:                                EWorldUtil.hotswap("eworld.service");
155:                            }
156:                        }
157:                    }
158:                } catch (InterruptedException e) {
159:                    throw new WrappedRuntimeException(e);
160:                }
161:            }
162:
163:            /**
164:             * Cache aspect.
165:             */
166:            public static class CacheAspect {
167:
168:                /** a static cache to flush it from the outside for demo purpose. Safe since aspect is singleton */
169:                public static Map s_cache = new HashMap();
170:
171:                public Object cache(final JoinPoint joinPoint) throws Throwable {
172:                    MethodRtti mrtti = (MethodRtti) joinPoint.getRtti();
173:                    Integer parameter = (Integer) mrtti.getParameterValues()[0];
174:                    Integer cachedValue = (Integer) s_cache.get(parameter);
175:                    if (cachedValue == null) {
176:                        System.err.println("not in cache");
177:                        Object newValue = joinPoint.proceed(); // not found => calculate
178:                        s_cache.put(parameter, newValue);
179:                        return newValue;
180:                    } else {
181:                        System.err.println("using cache: " + cachedValue);
182:                        return cachedValue; // return cached value
183:                    }
184:                }
185:            }
186:
187:            /**
188:             * Trace aspect.
189:             */
190:            public static class TraceAspect {
191:                private int m_level = 0;
192:
193:                public Object trace(final JoinPoint joinPoint) throws Throwable {
194:                    MemberSignature signature = (MemberSignature) joinPoint
195:                            .getSignature();
196:                    indent();
197:                    System.out.println("--> "
198:                            + signature.getDeclaringType().getName() + "::"
199:                            + signature.getName());
200:                    m_level++;
201:                    final Object result = joinPoint.proceed();
202:                    m_level--;
203:                    indent();
204:                    System.out.println("<-- "
205:                            + signature.getDeclaringType().getName() + "::"
206:                            + signature.getName());
207:                    return result;
208:                }
209:
210:                private void indent() {
211:                    for (int i = 0; i < m_level; i++) {
212:                        System.out.print("  ");
213:                    }
214:                }
215:            }
216:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.