Source Code Cross Referenced for XRouteManager.java in  » XML-UI » XUI » net » xoetrope » optional » 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 » XML UI » XUI » net.xoetrope.optional.service 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.xoetrope.optional.service;
002:
003:        import java.util.Hashtable;
004:
005:        /**
006:         * Constructs service call routes from an XML descriptor. XRouteManager caches
007:         * routes so that they can be resused once constructed.
008:         *
009:         * The startup properties file lists the datasets file in its ModelData entry.
010:         *
011:         * e.g.
012:         * <PRE>
013:         * UseWindow=false
014:         * ClientWidth=800
015:         * ClientHeight=600
016:         * StartPackage=net.xoetrope.myPackage
017:         * StartClass=myStartScreen
018:         * Title=My Application
019:         * StyleFile=myStyles.txt
020:         * <B>ModelData=datasets.xml</B>
021:         * CenterWin=false
022:         * XDataSourceClass=net.xoetrope.xlib.data.XLibDataSource
023:         * </PRE>
024:         *
025:         * The dataset then points to a datasource, usually called routes.xml.
026:         *
027:         * <PRE>
028:         * &LT;?xml version="1.0" encoding="UTF-8"?&GT;
029:         * &LT;DataSources&GT;
030:         *         &LT;DataSource name="Tables" type="database" filename="tables.xml"/&GT;
031:         *         &LT;DataSource name="ListValues" filename="staticdata.xml"/&GT;
032:         *         &LT;DataSource name="Routes" type="routing" filename="routes.xml"/&GT;
033:         *         &LT;DataSource name="Services" type="service" filename="services.xml"/&GT;
034:         * &LT;/DataSources&GT;
035:         * </PRE>
036:         *
037:         * This XML
038:         * file describes each named route as a series of layers with separate entries
039:         * for the client (local) and server (remote) sides.
040:         *
041:         * <PRE>
042:         *  &LT;?xml version="1.0" encoding="UTF-8"?&GT;
043:         *  &LT;Routes&GT;
044:         *          &LT;route name="myRemoteService"&GT;
045:         *                  &LT;layer class="net.xoetrope.service.XHttpClientServiceProxy" url="http://localhost:8080/xserviceservlet" urlEncode="false"/&GT;
046:         *          &LT;/route&GT;
047:         *          &LT;route name="<B>myCalculationService</B>"&GT;
048:         *                  &LT;layer class="net.xoetrope.service.test.CalcTestService"/&GT;
049:         *          &LT;/route&GT;
050:         *  &LT;/Routes&GT;
051:         * </PRE>
052:         *
053:         * The route manager is used in conjunction with the XServiceModelNode as the
054:         * following fragement of test code looking up and invoking shows:
055:         *
056:         * <PRE>
057:         *     XRouteManager routeMgr = XRouteManager.getInstance();
058:         *     XServiceModelNode node = new XServiceModelNode();
059:         *     try {
060:         *       node.setupService( "getName", routeMgr.getRoute( "<B>myCalculationService</B>" ), null );
061:         *       Object result = node.get();
062:         *       assertTrue( result.toString().compareTo( CalcTestService.class.getName() ) == 0 );
063:         *       ...
064:         * </PRE>
065:         *
066:         * Normally this method of invoking a service would not be used as the services
067:         * can more easily be invoked by looking up the XServiceModelNode in the overall
068:         * XModel. The nodes in the XModel are configured via the <i>Services</i>
069:         * datasource as follow:
070:         *
071:         * <PRE>
072:         * &LT;?xml version="1.0" encoding="UTF-8"?&GT;
073:         * &LT;Services&GT;
074:         *         &LT;service name="<B>calcVolume</B>" route="remote" processor="net.xoetrope.service.test.CalcTestService"&GT;
075:         *                 &LT;arg name="roomWidth" type="double"/&GT;
076:         *                 &LT;arg name="roomLength" type="double"/&GT;
077:         *                 &LT;arg name="roomHeight" type="double"/&GT;
078:         *                 &LT;return type="double"/&GT;
079:         *         &LT;/service&GT;
080:         *         &LT;service name="calcRepayments" route="remote"&GT;
081:         *                 &LT;arg name="term" type="int" mandatory="true"/&GT;
082:         *                 &LT;arg name="value" type="double" mandatory="true"/&GT;
083:         *                 &LT;arg name="deposit" type="double" mandatory="false"/&GT;
084:         *         &LT;/service&GT;
085:         *         &LT;service name="getName" route="calcRoute"&GT;
086:         *                 &LT;return type="String"/&GT;
087:         *         &LT;/service&GT;
088:         *         &LT;service name="getNumber" route="calcRoute"&GT;
089:         *                 &LT;return type="int"/&GT;
090:         *         &LT;/service&GT;
091:         * &LT;/Services&GT;
092:         * </PRE>
093:         * <BR>
094:         * The service can then be invoked via the model as in the following example.
095:         * <PRE>
096:         *  try {
097:         *    XServiceModelNode node = (XServiceModelNode)XModel.getInstance().get( "<B>calcVolume</B>" );
098:         *    node.setAttribValue( 0, new Double( 3.9 ));
099:         *    node.setAttribValue( 1, new Double( 4.6 ));
100:         *    node.setAttribValue( 2, new Double( 2.5 ));
101:         *    Object result = node.get();
102:         *    assertTrue( result instanceof Double );
103:         *    assertTrue( ((Double)result).doubleValue() == 101.0  );
104:         *  }
105:         *  catch ( Exception ex ) {
106:         *    ex.printStackTrace();
107:         *  }
108:         *</PRE>
109:         *
110:         * Note that on the server side of a remote call the services may need to be
111:         * implemented in the reverse order and in a slightly different order i.e. the
112:         * server side will have to provide an implementation of the service.
113:         *
114:         * <p>Copyright (c) Xoetrope Ltd. 2001-2003</p>
115:         * $Revision: 1.1 $
116:         */
117:        public class XRouteManager {
118:            public static final int CLIENT_SIDE = 0;
119:            public static final int SERVER_SIDE = 1;
120:
121:            protected static XRouteManager routeMgr;
122:            protected static Hashtable routes;
123:            private static int side = CLIENT_SIDE;
124:
125:            protected XRouteManager() {
126:                routes = new Hashtable();
127:            }
128:
129:            public static XRouteManager getInstance() {
130:                if (routeMgr == null) {
131:                    routeMgr = new XRouteManager();
132:                }
133:
134:                return routeMgr;
135:            }
136:
137:            /**
138:             * Flags the route manager as being on the client or server side
139:             * @param side indicates client (0) or server(1) side
140:             */
141:            public static void setSide(int s) {
142:                side = s;
143:            }
144:
145:            /**
146:             * @param route the route name
147:             * @return the service route
148:             */
149:            public static ServiceProxy getRoute(String route)
150:                    throws XServiceProxyNotFoundException {
151:                return getRoute(route, null);
152:            }
153:
154:            /**
155:             * @param route the route name
156:             * @param serviceImplementationClass the service implementation class if this
157:             * is a new route or if a route is being customized by an additional service
158:             * class
159:             * @return the service route
160:             */
161:            public static ServiceProxy getRoute(String route,
162:                    String serviceImplementationClass)
163:                    throws XServiceProxyNotFoundException {
164:                ServiceProxy spParent = null;
165:
166:                try {
167:                    Hashtable[] layers = (Hashtable[]) routes.get(route);
168:                    if (layers != null) {
169:                        // Reverse the iteration if on the server side
170:                        int inc = side == CLIENT_SIDE ? 1 : -1;
171:                        int start = side == CLIENT_SIDE ? 0 : layers.length - 1;
172:                        int next = side == CLIENT_SIDE ? 1 : layers.length - 2;
173:                        int last = (side == CLIENT_SIDE) ? layers.length : -1;
174:
175:                        // Build the first element in the stack
176:                        spParent = spParent = (ServiceProxy) Class.forName(
177:                                (String) layers[start].get("class"))
178:                                .newInstance();
179:                        ;
180:                        spParent.setRouteName(route);
181:                        spParent.setAttributes(layers[start]);
182:
183:                        ServiceProxy spNext = spParent;
184:
185:                        // Build the remainder of the stack
186:                        while (next != last) {
187:                            ServiceProxy sp = (ServiceProxy) Thread
188:                                    .currentThread().getContextClassLoader()
189:                                    .getClass().forName(
190:                                            (String) layers[next].get("class"))
191:                                    .newInstance();
192:                            sp.setRouteName(route);
193:                            sp.setAttributes(layers[next]);
194:
195:                            spNext.setNextProxy(sp);
196:
197:                            spNext = sp;
198:                            next += inc;
199:                        }
200:
201:                        // Set the implementation class as the last node as it should be the last
202:                        // thing invoked, after alll the stack elements have done their thin
203:                        // Load the implementation class
204:                        ServiceProxy implementationProxy = null;
205:                        try {
206:                            if (serviceImplementationClass != null) {
207:                                implementationProxy = (ServiceProxy) Class
208:                                        .forName(serviceImplementationClass)
209:                                        .newInstance();
210:                                implementationProxy.setRouteName(route);
211:
212:                                spNext.setNextProxy(implementationProxy);
213:                            }
214:                        } catch (Exception ex) {
215:                        }
216:                    }
217:                } catch (Exception ex) {
218:                    System.err.println("Route not found: " + route);
219:                    throw new XServiceProxyNotFoundException();
220:                }
221:
222:                if (spParent == null) {
223:                    System.err.println("Route not found: " + route);
224:                    throw new XServiceProxyNotFoundException();
225:                }
226:
227:                return spParent;
228:            }
229:
230:            public static void addRoute(String name, Hashtable[] layers) {
231:                routes.put(name, layers);
232:            }
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.