KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > container > FilterFactoryComponentAdapter


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or modify it under the terms of the
7  * GNU Library General Public License as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
11  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License along with this
15  * library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
16  * USA.
17  *
18  */

19
20 package org.jacorb.notification.container;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.logger.Logger;
24 import org.jacorb.notification.IContainer;
25 import org.jacorb.notification.conf.Attributes;
26 import org.jacorb.notification.conf.Default;
27 import org.jacorb.notification.filter.FilterFactoryImpl;
28 import org.jacorb.notification.filter.DefaultFilterFactoryDelegate;
29 import org.jacorb.notification.util.LogUtil;
30 import org.jacorb.notification.util.PatternWrapper;
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.CosNotifyFilter.FilterFactory;
33 import org.omg.CosNotifyFilter.FilterFactoryHelper;
34 import org.picocontainer.MutablePicoContainer;
35 import org.picocontainer.PicoContainer;
36 import org.picocontainer.defaults.AbstractComponentAdapter;
37
38 class FilterFactoryComponentAdapter extends AbstractComponentAdapter
39 {
40     private static final long serialVersionUID = 1L;
41     
42     private final Logger logger_ = LogUtil.getLogger(getClass().getName());
43
44     public FilterFactoryComponentAdapter()
45     {
46         super(FilterFactory.class, FilterFactory.class);
47     }
48
49     public void verify(PicoContainer container)
50     {
51         // no operation
52
}
53
54     public Object JavaDoc getComponentInstance(PicoContainer container)
55     {
56         if (!Default.DEFAULT_FILTER_FACTORY.equals(getFilterFactoryLocation(container)))
57         {
58             try
59             {
60                 return lookupFilterFactory(container);
61             } catch (Exception JavaDoc e)
62             {
63                 logger_
64                         .info(
65                                 "Could not resolve FilterFactory. Will fall back to builtin FilterFactory.",
66                                 e);
67             }
68         }
69
70         return newFilterFactory(container);
71     }
72
73     private FilterFactory newFilterFactory(PicoContainer container)
74     {
75         // force Classloader to load Class PatternWrapper.
76
// PatternWrapper may cause a ClassNotFoundException if
77
// running on < JDK1.4 and gnu.regexp is NOT installed.
78
// Therefor the Error should occur as _early_ as possible.
79
PatternWrapper.class.getName();
80
81         final MutablePicoContainer _parent = (MutablePicoContainer) container;
82
83         final MutablePicoContainer _container = _parent.makeChildContainer();
84
85         _container.registerComponentImplementation(DefaultFilterFactoryDelegate.class);
86
87         _container.registerComponentImplementation(FilterFactoryImpl.class);
88         
89         _container.registerComponentInstance(IContainer.class, new IContainer()
90         {
91             public MutablePicoContainer getContainer()
92             {
93                 return _container;
94             }
95
96             public void destroy()
97             {
98                 _parent.removeChildContainer(_container);
99             }
100         });
101
102         FilterFactoryImpl servant = (FilterFactoryImpl) _container
103                 .getComponentInstanceOfType(FilterFactoryImpl.class);
104
105         return FilterFactoryHelper.narrow(servant.activate());
106     }
107
108     private FilterFactory lookupFilterFactory(PicoContainer container)
109     {
110         String JavaDoc _filterFactoryConf = getFilterFactoryLocation(container);
111
112         ORB JavaDoc orb = (ORB JavaDoc) container.getComponentInstance(ORB JavaDoc.class);
113
114         return FilterFactoryHelper.narrow(orb.string_to_object(_filterFactoryConf));
115     }
116
117     private String JavaDoc getFilterFactoryLocation(PicoContainer container)
118     {
119         Configuration config = (Configuration) container.getComponentInstance(Configuration.class);
120
121         String JavaDoc _location = config.getAttribute(Attributes.FILTER_FACTORY,
122                 Default.DEFAULT_FILTER_FACTORY);
123
124         return _location;
125     }
126 }
Popular Tags