Source Code Cross Referenced for HandlerChainBuilder.java in  » Web-Services-apache-cxf-2.0.1 » frontend » org » apache » cxf » jaxws » handler » 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 Services apache cxf 2.0.1 » frontend » org.apache.cxf.jaxws.handler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */package org.apache.cxf.jaxws.handler;
019:
020:        import java.lang.reflect.InvocationTargetException;
021:        import java.lang.reflect.Method;
022:        import java.net.URL;
023:        import java.util.ArrayList;
024:        import java.util.HashMap;
025:        import java.util.List;
026:        import java.util.Map;
027:        import java.util.ResourceBundle;
028:        import java.util.logging.Level;
029:        import java.util.logging.Logger;
030:
031:        import javax.xml.ws.WebServiceException;
032:        import javax.xml.ws.handler.Handler;
033:        import javax.xml.ws.handler.LogicalHandler;
034:
035:        import org.apache.cxf.Bus;
036:        import org.apache.cxf.common.injection.ResourceInjector;
037:        import org.apache.cxf.common.logging.LogUtils;
038:        import org.apache.cxf.jaxws.javaee.HandlerChainType;
039:        import org.apache.cxf.jaxws.javaee.ParamValueType;
040:        import org.apache.cxf.jaxws.javaee.PortComponentHandlerType;
041:        import org.apache.cxf.resource.ResourceManager;
042:        import org.apache.cxf.resource.ResourceResolver;
043:
044:        public class HandlerChainBuilder {
045:            static final Logger LOG = LogUtils
046:                    .getL7dLogger(HandlerChainBuilder.class);
047:            private static final ResourceBundle BUNDLE = LOG
048:                    .getResourceBundle();
049:
050:            private Bus bus;
051:            private boolean handlerInitEnabled = true;
052:
053:            public HandlerChainBuilder(Bus aBus) {
054:                bus = aBus;
055:            }
056:
057:            public HandlerChainBuilder() {
058:                this (null);
059:            }
060:
061:            public List<Handler> buildHandlerChainFromConfiguration(
062:                    HandlerChainType hc) {
063:                if (null == hc) {
064:                    return null;
065:                }
066:                return sortHandlers(buildHandlerChain(hc,
067:                        getHandlerClassLoader()));
068:            }
069:
070:            // methods used by Geronimo to allow configuring things themselves
071:            public void setHandlerInitEnabled(boolean b) {
072:                handlerInitEnabled = b;
073:            }
074:
075:            public boolean isHandlerInitEnabled() {
076:                return handlerInitEnabled;
077:            }
078:
079:            /**
080:             * sorts the handlers into correct order. All of the logical handlers first
081:             * followed by the protocol handlers
082:             * 
083:             * @param handlers
084:             * @return sorted list of handlers
085:             */
086:            public List<Handler> sortHandlers(List<Handler> handlers) {
087:
088:                List<LogicalHandler> logicalHandlers = new ArrayList<LogicalHandler>();
089:                List<Handler> protocolHandlers = new ArrayList<Handler>();
090:
091:                for (Handler handler : handlers) {
092:                    if (handler instanceof  LogicalHandler) {
093:                        logicalHandlers.add((LogicalHandler) handler);
094:                    } else {
095:                        protocolHandlers.add(handler);
096:                    }
097:                }
098:
099:                List<Handler> sortedHandlers = new ArrayList<Handler>();
100:                sortedHandlers.addAll(logicalHandlers);
101:                sortedHandlers.addAll(protocolHandlers);
102:                return sortedHandlers;
103:            }
104:
105:            protected ClassLoader getHandlerClassLoader() {
106:                return getClass().getClassLoader();
107:            }
108:
109:            protected List<Handler> buildHandlerChain(HandlerChainType hc,
110:                    ClassLoader classLoader) {
111:                List<Handler> handlerChain = new ArrayList<Handler>();
112:                for (PortComponentHandlerType ht : hc.getHandler()) {
113:                    try {
114:                        LOG.log(Level.FINE, "loading handler", trimString(ht
115:                                .getHandlerName().getValue()));
116:
117:                        Class<? extends Handler> handlerClass = Class.forName(
118:                                trimString(ht.getHandlerClass().getValue()),
119:                                true, classLoader).asSubclass(Handler.class);
120:
121:                        Handler handler = handlerClass.newInstance();
122:                        LOG.fine("adding handler to chain: " + handler);
123:                        configureHandler(handler, ht);
124:                        handlerChain.add(handler);
125:                    } catch (Exception e) {
126:                        throw new WebServiceException(BUNDLE
127:                                .getString("HANDLER_INSTANTIATION_EXC"), e);
128:                    }
129:                }
130:                return handlerChain;
131:            }
132:
133:            /**
134:             * Resolve handler chain configuration file associated with the given class
135:             * 
136:             * @param clz
137:             * @param filename
138:             * @return A URL object or null if no resource with this name is found
139:             */
140:            protected URL resolveHandlerChainFile(Class clz, String filename) {
141:                URL handlerFile = clz.getResource(filename);
142:                if (handlerFile == null) {
143:                    //the file location might be an absolute java.net.URL in externalForm.
144:                    try {
145:                        handlerFile = new URL(filename);
146:                        //test if the URL can be opened
147:                        handlerFile.openStream();
148:                    } catch (Exception e) {
149:                        //do nothing
150:                    }
151:                }
152:                return handlerFile;
153:            }
154:
155:            private void configureHandler(Handler handler,
156:                    PortComponentHandlerType h) {
157:                if (!handlerInitEnabled) {
158:                    return;
159:                }
160:
161:                if (h.getInitParam().size() == 0) {
162:                    return;
163:                }
164:
165:                Map<String, String> params = new HashMap<String, String>();
166:
167:                for (ParamValueType param : h.getInitParam()) {
168:                    params.put(trimString(param.getParamName() == null ? null
169:                            : param.getParamName().getValue()),
170:                            trimString(param.getParamValue() == null ? null
171:                                    : param.getParamValue().getValue()));
172:                }
173:
174:                Method initMethod = getInitMethod(handler);
175:                if (initMethod != null) {
176:                    initializeViaInitMethod(handler, params, initMethod);
177:                } else {
178:                    initializeViaInjection(handler, params);
179:                }
180:            }
181:
182:            private void initializeViaInjection(Handler handler,
183:                    final Map<String, String> params) {
184:                if (bus != null) {
185:                    ResourceManager resMgr = bus
186:                            .getExtension(ResourceManager.class);
187:                    List<ResourceResolver> resolvers = resMgr
188:                            .getResourceResolvers();
189:                    resolvers.add(new InitParamResourceResolver(params));
190:                    ResourceInjector resInj = new ResourceInjector(resMgr,
191:                            resolvers);
192:                    resInj.inject(handler);
193:                }
194:            }
195:
196:            private void initializeViaInitMethod(Handler handler,
197:                    Map<String, String> params, Method init) {
198:                try {
199:                    init.invoke(handler, params);
200:                } catch (InvocationTargetException ex) {
201:                    Throwable t = ex.getCause() != null ? ex.getCause() : ex;
202:                    LogUtils.log(LOG, Level.WARNING,
203:                            "INIT_METHOD_THREW_EXCEPTION", t, handler
204:                                    .getClass());
205:                } catch (IllegalAccessException ex) {
206:                    LOG.log(Level.SEVERE, "CANNOT_ACCESS_INIT", handler
207:                            .getClass());
208:                }
209:            }
210:
211:            private Method getInitMethod(Handler handler) {
212:                Method m = null;
213:                try {
214:                    m = handler.getClass().getMethod("init", Map.class);
215:                } catch (NoSuchMethodException ex) {
216:                    // emtpy
217:                }
218:                return m;
219:            }
220:
221:            private String trimString(String str) {
222:                return str != null ? str.trim() : null;
223:            }
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.