Source Code Cross Referenced for InterceptorManager.java in  » Portal » stringbeans-3.5 » com » nabhinc » ws » server » 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 » Portal » stringbeans 3.5 » com.nabhinc.ws.server 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * (C) Copyright 2005 Nabh Information Systems, Inc.
003:         *
004:         * This program is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU General Public License 
006:         * as published by the Free Software Foundation; either version 2
007:         * of the License, or (at your option) any later version.
008:         * 
009:         * This program is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of 
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
012:         * GNU General Public License for more details.
013:         * 
014:         * You should have received a copy of the GNU General Public License
015:         * along with this program; if not, write to the Free Software 
016:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017:         * 
018:         */
019:        package com.nabhinc.ws.server;
020:
021:        import java.io.IOException;
022:        import java.io.Writer;
023:        import java.util.ArrayList;
024:        import java.util.Collections;
025:        import java.util.HashMap;
026:        import java.util.Hashtable;
027:        import java.util.List;
028:        import java.util.Vector;
029:
030:        import javax.servlet.ServletException;
031:
032:        import org.apache.commons.logging.Log;
033:        import org.apache.commons.logging.LogFactory;
034:        import org.w3c.dom.Element;
035:
036:        import com.nabhinc.util.StringUtil;
037:        import com.nabhinc.util.XMLUtil;
038:        import com.nabhinc.ws.core.WebServiceException;
039:
040:        /**
041:         * Provides static methods to handle interceptor loading/unloading, 
042:         * chain creation, etc.
043:         * 
044:         * @author Padmanabh Dabke
045:         * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
046:         */
047:        public class InterceptorManager {
048:
049:            /**
050:             * Logger
051:             */
052:            private static Log imLogger = LogFactory
053:                    .getLog(InterceptorManager.class);
054:
055:            private static WebServiceContext imServiceContext = null;
056:            /**
057:             * List of <code>InterceptorInfo</code> objects.
058:             */
059:            protected static List imInterceptorList = new ArrayList();
060:
061:            /**
062:             * Maintains mapping from interceptor name -> InterceptorInfo
063:             */
064:            protected static Hashtable imInterceptorMap = new Hashtable();
065:
066:            /**
067:             * A comma separated list of interceptor names specified in the
068:             * configuration. These interceptors are added in the beginning
069:             * of all interceptor chains.
070:             */
071:            protected static String imBeforeInterceptorStr = null;
072:
073:            /**
074:             * A comma separated list of interceptor names specified in the
075:             * configuration. These interceptors are added at the end of
076:             * of all interceptor chains.
077:             */
078:            protected static String imAfterInterceptorStr = null;
079:
080:            /**
081:             * A list of InterceptorInfo objects. These interceptors are added
082:             * in the beginning of all interceptor chains.
083:             */
084:            protected static List imBeforeInterceptorList = new ArrayList();
085:
086:            /**
087:             * A list of InterceptorInfo objects. These interceptors are added
088:             * at the end of of all interceptor chains.
089:             */
090:
091:            protected static List imAfterInterceptorList = new ArrayList();
092:
093:            /**
094:             * List of ServiceInterceptorMapping objects.
095:             */
096:            protected static List imServiceInterceptorMappingList = new ArrayList();
097:
098:            /**
099:             * Maintains mapping from "<service-name>[:<method-name>]" to
100:             * interceptor chains.
101:             */
102:            private static HashMap imServiceInterceptorChainMap = new HashMap();
103:
104:            /**
105:             * Global interceptor chain used for services that do not have
106:             * any service/method specific interceptors.
107:             */
108:            private static InterceptorInfo[] imGlobalChain = new InterceptorInfo[0];
109:
110:            /**
111:             * Represents a service-interceptor mapping entry that associates a list
112:             * of interceptors with a service and optionally a set of methods.
113:             */
114:            public static class ServiceInterceptorMapping {
115:                /**
116:                 * Web service name
117:                 */
118:                public String serviceName = null;
119:
120:                /**
121:                 * Comma seperated list of method names as specified in the
122:                 * configuration. If this is null or "*", the interceptor chain
123:                 * is attached to all methods on the service.
124:                 */
125:                public String methods = null;
126:
127:                /**
128:                 * Comma separated list of interceptor names to be attached
129:                 * to the service
130:                 */
131:                public String interceptorStr = null;
132:
133:                /**
134:                 * Interceptor chain computed from the list of interceptors
135:                 * specified in the configuration.
136:                 */
137:                public InterceptorInfo[] interceptors = null;
138:
139:                /**
140:                 * The flag that indicates if the interceptors specified
141:                 * in this mapping override the global setting. The default 
142:                 * behavior is to insert the configured list between
143:                 * global "before" and "after" interceptors.
144:                 */
145:                public boolean overrideGlobalInterceptorChain = false;
146:
147:                /**
148:                 * Constructs all <code>ServiceInterceptorMapping</code> objects
149:                 * based on the configuration and puts them in the interceptor
150:                 * chain map.
151:                 * @param config Service-Interceptor mapping configuration
152:                 * @throws ServletException
153:                 */
154:                protected static void initMapping(Element config)
155:                        throws WebServiceException {
156:                    Element[] mapElems = XMLUtil
157:                            .getSubElements(
158:                                    config,
159:                                    WebServiceServerConstants.SERVICE_INTERCEPTOR_MAPPING_TAG);
160:                    if (mapElems == null)
161:                        return;
162:
163:                    for (int i = 0; i < mapElems.length; i++) {
164:                        ServiceInterceptorMapping mapping = new ServiceInterceptorMapping();
165:                        mapping.init(mapElems[i]);
166:                        imServiceInterceptorMappingList.add(mapping);
167:                    }
168:                }
169:
170:                /**
171:                 * Serializes all service-interceptor mapping to XML.
172:                 * @param indent Indent used to format XML
173:                 * @param delta Spaces added to go to next indentation level
174:                 * @param w Writer
175:                 * @throws IOException
176:                 */
177:                protected static void serializeMapping(String indent,
178:                        String delta, Writer w) throws IOException {
179:
180:                    for (int i = 0; i < imServiceInterceptorMappingList.size(); i++) {
181:                        ServiceInterceptorMapping mapping = (ServiceInterceptorMapping) imServiceInterceptorMappingList
182:                                .get(i);
183:                        mapping.serialize(indent, delta, w);
184:
185:                    }
186:                }
187:
188:                /**
189:                 * Creates a <code>ServiceInterceptorMapping</code> object based on the supplied
190:                 * configuration.
191:                 * @param config Service-interceptor mapping configuration
192:                 * @throws ServletException
193:                 */
194:                protected void init(Element config) throws WebServiceException {
195:                    String sName = XMLUtil.getSubElementText(config,
196:                            WebServiceServerConstants.SERVICE_TAG);
197:                    String ms = XMLUtil.getSubElementText(config,
198:                            WebServiceServerConstants.METHODS_TAG);
199:                    String intStr = XMLUtil.getSubElementText(config,
200:                            WebServiceServerConstants.INTERCEPTORS_TAG);
201:                    if (intStr == null) {
202:                        throw new WebServiceException(
203:                                "Interceptor names must be specified in service-interceptor mapping.");
204:                    }
205:                    if (sName == null) {
206:                        throw new WebServiceException(
207:                                "Service name must be specified in service-interceptor mapping.");
208:                    }
209:                    String[] names = StringUtil.split(intStr, ",");
210:                    boolean overrideFlag = XMLUtil
211:                            .getSubElement(
212:                                    config,
213:                                    WebServiceServerConstants.OVERRIDE_GLOBAL_CHAIN_TAG) == null ? false
214:                            : true;
215:                    _init(sName, ms, intStr, names, overrideFlag);
216:                }
217:
218:                private void _init(String sName, String ms, String intStr,
219:                        String[] names, boolean overrideGlobal)
220:                        throws WebServiceException {
221:                    serviceName = sName;
222:                    methods = ms;
223:                    interceptorStr = intStr;
224:                    overrideGlobalInterceptorChain = overrideGlobal;
225:                    interceptors = new InterceptorInfo[names.length];
226:                    for (int i = 0; i < names.length; i++) {
227:                        InterceptorInfo info = (InterceptorInfo) imInterceptorMap
228:                                .get(names[i]);
229:                        if (info == null) {
230:                            throw new WebServiceException(
231:                                    "Non-existent interceptor specified in service-interceptor mapping: "
232:                                            + names[i] + ".");
233:                        }
234:                        interceptors[i] = info;
235:                    }
236:
237:                    if (methods == null || methods.equals(""))
238:                        methods = "*";
239:                }
240:
241:                /**
242:                 * Serializes a <code>ServiceInterceptorMapping</code> object.
243:                 * @param indent Indent used to format XML
244:                 * @param delta Spaces added to go to next indentation level
245:                 * @param w Writer
246:                 * @throws IOException
247:                 */
248:                protected void serialize(String indent, String delta, Writer w)
249:                        throws IOException {
250:                    XMLUtil
251:                            .writeElementStart(
252:                                    indent,
253:                                    WebServiceServerConstants.SERVICE_INTERCEPTOR_MAPPING_TAG,
254:                                    w);
255:                    String indent1 = indent + delta;
256:                    XMLUtil.writeElement(indent1,
257:                            WebServiceServerConstants.SERVICE_TAG, serviceName,
258:                            w);
259:                    XMLUtil.writeElement(indent1,
260:                            WebServiceServerConstants.METHODS_TAG, methods, w);
261:                    XMLUtil.writeElement(indent1,
262:                            WebServiceServerConstants.INTERCEPTORS_TAG,
263:                            interceptorStr, w);
264:                    if (overrideGlobalInterceptorChain)
265:                        XMLUtil
266:                                .writeEmptyElement(
267:                                        indent1,
268:                                        WebServiceServerConstants.OVERRIDE_GLOBAL_CHAIN_TAG,
269:                                        w);
270:                    XMLUtil
271:                            .writeElementEnd(
272:                                    indent,
273:                                    WebServiceServerConstants.SERVICE_INTERCEPTOR_MAPPING_TAG,
274:                                    w);
275:                }
276:
277:                /**
278:                 * Removes specified interceptor from the mapping (if present). If 
279:                 * @param intInfo Interceptor to be removed.
280:                 * @return true if this mapping should be dropped since it doesn't have
281:                 * any interceptors after the removal
282:                 */
283:                protected boolean removeInterceptor(InterceptorInfo intInfo) {
284:                    int index = -1;
285:                    for (int i = 0; i < interceptors.length; i++) {
286:                        if (interceptors[i].equals(intInfo)) {
287:                            index = i;
288:                            break;
289:                        }
290:                    }
291:
292:                    if (index == -1)
293:                        return false;
294:                    if (interceptors.length == 1) {
295:                        interceptors = new InterceptorInfo[0];
296:                        return true;
297:                    }
298:                    InterceptorInfo[] newInfo = new InterceptorInfo[interceptors.length - 1];
299:                    for (int i = 0; i < index; i++) {
300:                        newInfo[i] = interceptors[i];
301:                    }
302:                    for (int i = index; i < newInfo.length; i++) {
303:                        newInfo[i] = interceptors[i + 1];
304:                    }
305:                    interceptors = newInfo;
306:                    interceptorStr = computeInterceptorStr(interceptors);
307:                    return false;
308:                }
309:
310:            }
311:
312:            /**
313:             * Serializes all interceptor-related configuration.
314:             * @param indent Indent used to format XML
315:             * @param delta Spaces added to go to next indentation level
316:             * @param w Writer
317:             * @throws IOException
318:             */
319:            public static void serialize(String indent, String delta, Writer w)
320:                    throws IOException {
321:
322:                XMLUtil.writeElementStart(indent,
323:                        WebServiceServerConstants.INTERCEPTORS_TAG, w);
324:
325:                String indent1 = indent + delta;
326:                for (int i = 0; i < imInterceptorList.size(); i++) {
327:                    InterceptorInfo inter = (InterceptorInfo) imInterceptorList
328:                            .get(i);
329:                    inter.serialize(indent1, delta, w);
330:                }
331:                XMLUtil.writeElementEnd(indent,
332:                        WebServiceServerConstants.INTERCEPTORS_TAG, w);
333:
334:                XMLUtil
335:                        .writeElementStart(
336:                                indent,
337:                                WebServiceServerConstants.SERVICE_INTERCEPTOR_MAPPING_CONFIG_TAG,
338:                                w);
339:                if (imBeforeInterceptorStr != null)
340:                    XMLUtil.writeElement(indent1,
341:                            WebServiceServerConstants.BEFORE_INTERCEPTORS_TAG,
342:                            imBeforeInterceptorStr, w);
343:                if (imAfterInterceptorStr != null)
344:                    XMLUtil.writeElement(indent1,
345:                            WebServiceServerConstants.AFTER_INTERCEPTORS_TAG,
346:                            imAfterInterceptorStr, w);
347:                ServiceInterceptorMapping.serializeMapping(indent1, delta, w);
348:                XMLUtil
349:                        .writeElementEnd(
350:                                indent,
351:                                WebServiceServerConstants.SERVICE_INTERCEPTOR_MAPPING_CONFIG_TAG,
352:                                w);
353:            }
354:
355:            /**
356:             * Initializes interceptor configuration and creates interceptor chains.
357:             * @param config Top level XML element in web services configuration file.
358:             * @param context Service context
359:             * @throws ServletException
360:             */
361:            protected static void init(Element config, WebServiceContext context)
362:                    throws WebServiceException {
363:                imServiceContext = context;
364:                readInterceptorConfig(config, context);
365:                populateState();
366:            }
367:
368:            /**
369:             * Extracts interceptor information from the XML element and creates <code>InterceptorInfo</code> and
370:             * <code>ServiceInterceptorMapping</code> objects.
371:             * @param config Top level XML element in web services configuration file.
372:             * @param context Server context
373:             * @throws ServletException
374:             */
375:            private static void readInterceptorConfig(Element config,
376:                    WebServiceContext context) throws WebServiceException {
377:                Element root = XMLUtil.getSubElement(config,
378:                        WebServiceServerConstants.INTERCEPTORS_TAG);
379:                if (root == null) {
380:                    throw new WebServiceException(
381:                            "No interceptor configuration defined.");
382:                }
383:
384:                Element[] intElems = XMLUtil.getSubElements(root,
385:                        WebServiceServerConstants.INTERCEPTOR_TAG);
386:                if (intElems == null || intElems.length == 0) {
387:                    imLogger
388:                            .warn("Interceptor configuration does not define any interceptors.");
389:                } else {
390:                    for (int i = 0; i < intElems.length; i++) {
391:                        InterceptorInfo intInfo = new InterceptorInfo();
392:                        intInfo.init(intElems[i], context);
393:                        _addInterceptor(intInfo);
394:                    }
395:                }
396:
397:                root = XMLUtil
398:                        .getSubElement(
399:                                config,
400:                                WebServiceServerConstants.SERVICE_INTERCEPTOR_MAPPING_CONFIG_TAG);
401:                if (root == null) {
402:                    throw new WebServiceException(
403:                            "Service-interceptor mapping not defined.");
404:                }
405:                imBeforeInterceptorStr = XMLUtil.getSubElementText(root,
406:                        WebServiceServerConstants.BEFORE_INTERCEPTORS_TAG);
407:                if (imBeforeInterceptorStr != null) {
408:                    String[] beforeNames = StringUtil.split(
409:                            imBeforeInterceptorStr, ",");
410:                    _setGlobalInterceptors(beforeNames,
411:                            imBeforeInterceptorList, "before-interceptors");
412:                }
413:
414:                imAfterInterceptorStr = XMLUtil.getSubElementText(root,
415:                        WebServiceServerConstants.AFTER_INTERCEPTORS_TAG);
416:                if (imAfterInterceptorStr != null) {
417:                    String[] afterNames = StringUtil.split(
418:                            imAfterInterceptorStr, ",");
419:                    _setGlobalInterceptors(afterNames, imAfterInterceptorList,
420:                            "after-interceptors");
421:                }
422:
423:                ServiceInterceptorMapping.initMapping(root);
424:
425:            }
426:
427:            private static void _setGlobalInterceptors(String[] beforeNames,
428:                    List intList, String intType) throws WebServiceException {
429:                if (beforeNames == null)
430:                    return;
431:                for (int i = 0; i < beforeNames.length; i++) {
432:
433:                    InterceptorInfo info = (InterceptorInfo) imInterceptorMap
434:                            .get(beforeNames[i]);
435:                    if (info == null) {
436:                        throw new WebServiceException(
437:                                "Non-existent interceptor specified in "
438:                                        + intType + ": " + beforeNames[i] + ".");
439:                    }
440:                    intList.add(info);
441:                }
442:
443:            }
444:
445:            /**
446:             * Returns a service-method specific chain if configured. If not, looks for a service
447:             * specific chain. Otherwise returns the global chain.
448:             * @param serviceName Web service name
449:             * @param methodName Method name
450:             * @return Interceptor chain to be applied to service/method combination
451:             */
452:            public synchronized static InterceptorInfo[] getInterceptorChain(
453:                    String serviceName, String methodName) {
454:                // First check if there is a chain associated with the method.
455:                InterceptorInfo[] chain = (InterceptorInfo[]) imServiceInterceptorChainMap
456:                        .get(serviceName + ":" + methodName);
457:                if (chain == null) {
458:                    // Now try to look for service-specific chain
459:                    chain = (InterceptorInfo[]) imServiceInterceptorChainMap
460:                            .get(serviceName);
461:                    if (chain == null) {
462:                        // Return global chain
463:                        return imGlobalChain;
464:                    } else {
465:                        return chain;
466:                    }
467:                } else {
468:                    return chain;
469:                }
470:            }
471:
472:            /**
473:             * Creates the global interceptor chain and service/method specific chains.
474:             *
475:             */
476:            protected synchronized static void populateState() {
477:
478:                // Reset state
479:                imGlobalChain = null;
480:                imServiceInterceptorChainMap.clear();
481:
482:                // Create global chain for services that do not have any service-specific interceptors.
483:                imGlobalChain = new InterceptorInfo[imBeforeInterceptorList
484:                        .size()
485:                        + imAfterInterceptorList.size()];
486:
487:                for (int i = 0; i < imBeforeInterceptorList.size(); i++) {
488:                    imGlobalChain[i] = (InterceptorInfo) imBeforeInterceptorList
489:                            .get(i);
490:                }
491:
492:                int offset = imBeforeInterceptorList.size();
493:                for (int i = 0; i < imAfterInterceptorList.size(); i++) {
494:                    imGlobalChain[i + offset] = (InterceptorInfo) imAfterInterceptorList
495:                            .get(i);
496:                }
497:
498:                // Create service/method specific chain mapping
499:                int globalLen = imBeforeInterceptorList.size()
500:                        + imAfterInterceptorList.size();
501:                for (int i = 0; i < imServiceInterceptorMappingList.size(); i++) {
502:                    ServiceInterceptorMapping mapping = (ServiceInterceptorMapping) imServiceInterceptorMappingList
503:                            .get(i);
504:                    InterceptorInfo[] chain = null;
505:                    if (mapping.overrideGlobalInterceptorChain) {
506:                        chain = mapping.interceptors;
507:                    } else {
508:                        chain = new InterceptorInfo[globalLen
509:                                + mapping.interceptors.length];
510:                        for (int j = 0; j < imBeforeInterceptorList.size(); j++) {
511:                            chain[j] = (InterceptorInfo) imBeforeInterceptorList
512:                                    .get(j);
513:                        }
514:
515:                        offset = imBeforeInterceptorList.size();
516:                        for (int j = 0; j < mapping.interceptors.length; j++) {
517:                            chain[j + offset] = mapping.interceptors[j];
518:                        }
519:
520:                        offset = imBeforeInterceptorList.size()
521:                                + mapping.interceptors.length;
522:                        for (int j = 0; j < imAfterInterceptorList.size(); j++) {
523:                            chain[j + offset] = (InterceptorInfo) imAfterInterceptorList
524:                                    .get(j);
525:                        }
526:                    }
527:
528:                    if (mapping.methods.equals("*")) {
529:                        imServiceInterceptorChainMap.put(mapping.serviceName,
530:                                chain);
531:                    } else {
532:                        String[] methodArray = StringUtil.split(
533:                                mapping.methods, ",");
534:                        for (int j = 0; j < methodArray.length; j++) {
535:                            imServiceInterceptorChainMap.put(
536:                                    mapping.serviceName + ":" + methodArray[j],
537:                                    chain);
538:                        }
539:                    }
540:                }
541:            }
542:
543:            /**
544:             * Helper function that creates a string suitable for writing to configuration
545:             * @param intVec Interceptor list
546:             * @return Configuration string
547:             */
548:            private static String computeInterceptorStr(List intVec) {
549:                if (intVec.size() == 0)
550:                    return null;
551:                StringBuffer sb = new StringBuffer();
552:                for (int i = 0; i < intVec.size(); i++) {
553:                    if (i != 0) {
554:                        sb.append(",");
555:                    }
556:                    sb.append(((InterceptorInfo) intVec.get(i)).name);
557:                }
558:                return sb.toString();
559:            }
560:
561:            /**
562:             * Helper function that creates a string suitable for writing to configuration
563:             * @param intArray Array of interceptors
564:             * @return Configuration string
565:             */
566:            private static String computeInterceptorStr(
567:                    InterceptorInfo[] intArray) {
568:                StringBuffer sb = new StringBuffer();
569:                for (int i = 0; i < intArray.length; i++) {
570:                    if (i != 0) {
571:                        sb.append(",");
572:                    }
573:                    sb.append(intArray[i].name);
574:                }
575:                return sb.toString();
576:            }
577:
578:            /**
579:             * Internal helper function that adds the interceptor to the list/map of inteceptors
580:             * and loads the interceptor if it is not configured to be manually loaded.
581:             * @param intInfo Interceptor to be added
582:             * @throws ServletException
583:             */
584:            private static void _addInterceptor(InterceptorInfo intInfo)
585:                    throws WebServiceException {
586:                imInterceptorList.add(intInfo);
587:                imInterceptorMap.put(intInfo.name, intInfo);
588:                if (!intInfo.manualLoad) {
589:                    try {
590:                        intInfo.load();
591:                    } catch (WebServiceException ex) {
592:                        if (intInfo.isCritical) {
593:                            imLogger.fatal(
594:                                    "Failed to load critical interceptor "
595:                                            + intInfo.name + ".", ex);
596:                            throw ex;
597:                        } else {
598:                            imLogger.error("Failed to load interceptor "
599:                                    + intInfo.name + ".", ex);
600:                        }
601:                    }
602:                }
603:
604:            }
605:
606:            /***********************************
607:             * Methods defined mainly to support UI manipulation of interceptor configuration.
608:             ***********************************/
609:            /**
610:             * Constructs a <code>InterceptorInfo</code> object from the request parameters. If the
611:             * interceptor is not manual load, loads it.
612:             * @param req
613:             * @param serverContext
614:             * @throws ServletException
615:             */
616:            public static void addInterceptor(InterceptorInfo intInfo)
617:                    throws WebServiceException, IOException {
618:                intInfo.webServiceContext = imServiceContext;
619:                _addInterceptor(intInfo);
620:                WebServiceServlet.getInstance().save();
621:            }
622:
623:            /**
624:             * Removes specified interceptor, any associated service-interceptor mapping.
625:             * Recomputes interceptor chains.
626:             * @param name Name of the interceptor to be removed.
627:             * @throws ServletException
628:             * @throws IOException
629:             */
630:            public static void deleteInterceptor(String name)
631:                    throws WebServiceException, IOException {
632:                InterceptorInfo intInfo = (InterceptorInfo) imInterceptorMap
633:                        .get(name);
634:                if (intInfo != null) {
635:                    intInfo.unload();
636:                    imInterceptorMap.remove(name);
637:                    imInterceptorList.remove(intInfo);
638:
639:                    imBeforeInterceptorList.remove(intInfo);
640:                    imBeforeInterceptorStr = computeInterceptorStr(imBeforeInterceptorList);
641:
642:                    imAfterInterceptorList.remove(intInfo);
643:                    imAfterInterceptorStr = computeInterceptorStr(imAfterInterceptorList);
644:
645:                    Vector deleteMappingVec = new Vector(4);
646:                    for (int i = 0; i < imServiceInterceptorMappingList.size(); i++) {
647:                        ServiceInterceptorMapping mapping = (ServiceInterceptorMapping) imServiceInterceptorMappingList
648:                                .get(i);
649:                        boolean deleteFlag = mapping.removeInterceptor(intInfo);
650:                        if (deleteFlag)
651:                            deleteMappingVec.addElement(mapping);
652:                    }
653:                    for (int i = 0; i < deleteMappingVec.size(); i++) {
654:                        imServiceInterceptorMappingList.remove(deleteMappingVec
655:                                .elementAt(i));
656:                    }
657:
658:                    populateState();
659:                    WebServiceServlet.getInstance().save();
660:                }
661:            }
662:
663:            /**
664:             * Sets the list of global "before" interceptors
665:             * @param newInt Array of interceptor names
666:             * @throws ServletException
667:             */
668:            public static void setBeforeInterceptors(String[] newInt)
669:                    throws WebServiceException, IOException {
670:                imBeforeInterceptorStr = StringUtil.join(newInt, ",");
671:                imBeforeInterceptorList.clear();
672:                _setGlobalInterceptors(newInt, imBeforeInterceptorList,
673:                        "before interceptors");
674:                populateState();
675:                WebServiceServlet.getInstance().save();
676:            }
677:
678:            /**
679:             * Sets the list of global "after" interceptors
680:             * @param newInt Array of interceptor names
681:             * @throws ServletException
682:             */
683:            public static void setAfterInterceptors(String[] newInt)
684:                    throws WebServiceException, IOException {
685:                imAfterInterceptorStr = StringUtil.join(newInt, ",");
686:                imAfterInterceptorList.clear();
687:                _setGlobalInterceptors(newInt, imAfterInterceptorList,
688:                        "after interceptors");
689:                populateState();
690:                WebServiceServlet.getInstance().save();
691:            }
692:
693:            /**
694:             * Sets before and after global interceptors, updates the state and saves it.
695:             * @param before Array of "before" interceptor names
696:             * @param after Array of "after" interceptor names
697:             * @throws WebServiceException
698:             * @throws IOException
699:             */
700:            public static void setGlobalInterceptors(String[] before,
701:                    String[] after) throws WebServiceException, IOException {
702:                imBeforeInterceptorStr = StringUtil.join(before, ",");
703:                imBeforeInterceptorList.clear();
704:                _setGlobalInterceptors(before, imBeforeInterceptorList,
705:                        "before interceptors");
706:                imAfterInterceptorStr = StringUtil.join(after, ",");
707:                imAfterInterceptorList.clear();
708:                _setGlobalInterceptors(after, imAfterInterceptorList,
709:                        "after interceptors");
710:                populateState();
711:                WebServiceServlet.getInstance().save();
712:
713:            }
714:
715:            /**
716:             * Adds a new service-interceptor mapping and re-populates state.
717:             * @param serviceName Web service name
718:             * @param methodNames Comma separated list of method names (can be null or "*")
719:             * @param interceptors Array of interceptor names
720:             * @throws ServletException
721:             */
722:            public static void addServiceInterceptorMapping(String serviceName,
723:                    String methodNames, String[] interceptors,
724:                    boolean overrideFlag) throws WebServiceException,
725:                    IOException {
726:                if (interceptors == null || interceptors.length == 0) {
727:                    throw new WebServiceException(
728:                            "You must specify at least one interceptor.");
729:                }
730:                String intStr = StringUtil.join(interceptors, ",");
731:                ServiceInterceptorMapping mapping = new ServiceInterceptorMapping();
732:                mapping._init(serviceName, methodNames, intStr, interceptors,
733:                        overrideFlag);
734:                imServiceInterceptorMappingList.add(mapping);
735:                populateState();
736:                WebServiceServlet.getInstance().save();
737:            }
738:
739:            /**
740:             * Removes a service-interceptor mapping at the specified position and re-populates
741:             * the state
742:             * @param pos Position of the mapping to be removed
743:             */
744:            public static void removeServiceInterceptorMappingAt(int pos)
745:                    throws WebServiceException, IOException {
746:                imServiceInterceptorMappingList.remove(pos);
747:                populateState();
748:                WebServiceServlet.getInstance().save();
749:            }
750:
751:            /**
752:             * Modifies an existing service-interceptor mapping and re-populates state.
753:             * @param serviceName Web service name
754:             * @param methodNames Comma separated list of method names (can be null or "*")
755:             * @param interceptors Array of interceptor names
756:             * @param pos Position of the mapping to be modified
757:             * @throws ServletException
758:             */
759:            public static void replaceServiceInterceptorMapping(
760:                    String serviceName, String methodNames,
761:                    String[] interceptors, boolean overrideFlag, int pos)
762:                    throws WebServiceException, IOException {
763:                if (interceptors == null || interceptors.length == 0) {
764:                    throw new WebServiceException(
765:                            "You must specify at least one interceptor.");
766:                }
767:                String intStr = StringUtil.join(interceptors, ",");
768:                ServiceInterceptorMapping mapping = (ServiceInterceptorMapping) imServiceInterceptorMappingList
769:                        .get(pos);
770:                mapping._init(serviceName, methodNames, intStr, interceptors,
771:                        overrideFlag);
772:                populateState();
773:                WebServiceServlet.getInstance().save();
774:
775:            }
776:
777:            public static List getBeforeInterceptorInfoList() {
778:                return Collections.unmodifiableList(imBeforeInterceptorList);
779:            }
780:
781:            public static List getAfterInterceptorInfoList() {
782:                return Collections.unmodifiableList(imAfterInterceptorList);
783:            }
784:
785:            public static List getInterceptorInfoList() {
786:                return Collections
787:                        .unmodifiableList(InterceptorManager.imInterceptorList);
788:            }
789:
790:            public static List getServiceInterceptorMappingList() {
791:                return Collections
792:                        .unmodifiableList(InterceptorManager.imServiceInterceptorMappingList);
793:            }
794:
795:            public static InterceptorInfo getInterceptorInfo(String name) {
796:                return (InterceptorInfo) InterceptorManager.imInterceptorMap
797:                        .get(name);
798:            }
799:
800:            public static void loadInterceptor(String name)
801:                    throws WebServiceException {
802:                InterceptorInfo intInfo = getInterceptorInfo(name);
803:                if (intInfo == null)
804:                    throw new WebServiceException("No such interceptor: "
805:                            + name + ".");
806:                intInfo.load();
807:            }
808:
809:            public static void unloadInterceptor(String name)
810:                    throws WebServiceException {
811:                InterceptorInfo intInfo = getInterceptorInfo(name);
812:                if (intInfo == null)
813:                    throw new WebServiceException("No such interceptor: "
814:                            + name + ".");
815:                intInfo.unload();
816:            }
817:
818:            public static void reloadInterceptor(String name)
819:                    throws WebServiceException {
820:                InterceptorInfo intInfo = getInterceptorInfo(name);
821:                if (intInfo == null)
822:                    throw new WebServiceException("No such interceptor: "
823:                            + name + ".");
824:                intInfo.reload();
825:            }
826:
827:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.