KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > ConfigLibrary


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.config;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.util.L10N;
34 import com.caucho.util.Log;
35 import com.caucho.vfs.ReadStream;
36 import com.caucho.vfs.Vfs;
37
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.lang.reflect.Modifier JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Library of static config functions.
50  */

51 public class ConfigLibrary {
52   private static final L10N L = new L10N(ConfigLibrary.class);
53   private static final Logger JavaDoc log = Log.open(ConfigLibrary.class);
54
55   private static EnvironmentLocal<ConfigLibrary> _localLibrary
56     = new EnvironmentLocal<ConfigLibrary>();
57
58   private HashMap JavaDoc<String JavaDoc,Method JavaDoc> _methodMap = new HashMap JavaDoc<String JavaDoc,Method JavaDoc>();
59
60   private ConfigLibrary()
61   {
62     configureLibrary();
63   }
64
65   public static ConfigLibrary getLocal()
66   {
67     return getLocal(Thread.currentThread().getContextClassLoader());
68   }
69
70   public static ConfigLibrary getLocal(ClassLoader JavaDoc loader)
71   {
72     loader = ClassLoader.getSystemClassLoader();
73     Thread JavaDoc thread = Thread.currentThread();
74     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
75
76     try {
77       thread.setContextClassLoader(loader);
78       
79       ConfigLibrary lib = _localLibrary.getLevel(loader);
80
81       if (lib == null) {
82     lib = new ConfigLibrary();
83
84     _localLibrary.set(lib, loader);
85       }
86
87       return lib;
88     } finally {
89       thread.setContextClassLoader(oldLoader);
90     }
91   }
92
93   /**
94    * Returns the method map.
95    */

96   public HashMap JavaDoc<String JavaDoc,Method JavaDoc> getMethodMap()
97   {
98     return _methodMap;
99   }
100
101   /**
102    * Scans the classpath for META-INF/services/com.caucho.quercus.QuercusModule
103    */

104   private void configureLibrary()
105   {
106     Thread JavaDoc thread = Thread.currentThread();
107     ClassLoader JavaDoc loader = thread.getContextClassLoader();
108     
109     try {
110       String JavaDoc library = "META-INF/services/com.caucho.config.ConfigLibrary";
111       Enumeration JavaDoc<URL JavaDoc> urls = loader.getResources(library);
112
113       while (urls.hasMoreElements()) {
114         URL JavaDoc url = urls.nextElement();
115
116         InputStream JavaDoc is = null;
117         ReadStream rs = null;
118         try {
119           is = url.openStream();
120           rs = Vfs.openRead(is);
121
122           parseServicesModule(rs);
123         } catch (Throwable JavaDoc e) {
124           log.log(Level.WARNING, e.toString(), e);
125         } finally {
126           if (rs != null)
127             rs.close();
128           if (is != null)
129             is.close();
130         }
131       }
132
133     } catch (Exception JavaDoc e) {
134       log.log(Level.WARNING, e.toString(), e);
135     }
136   }
137
138   /**
139    * Parses the services file, looking for PHP services.
140    */

141   private void parseServicesModule(ReadStream in)
142     throws IOException JavaDoc, ClassNotFoundException JavaDoc,
143        IllegalAccessException JavaDoc, InstantiationException JavaDoc
144   {
145     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
146     String JavaDoc line;
147
148     while ((line = in.readLine()) != null) {
149       int p = line.indexOf('#');
150
151       if (p >= 0)
152         line = line.substring(0, p);
153
154       line = line.trim();
155
156       if (line.length() > 0) {
157         String JavaDoc className = line;
158
159         Class JavaDoc cl = Class.forName(className, false, loader);
160
161         introspectLibraryClass(cl);
162       }
163     }
164   }
165
166   /**
167    * Introspects the module class for functions.
168    *
169    * @param cl the class to introspect.
170    */

171   private void introspectLibraryClass(Class JavaDoc cl)
172     throws IllegalAccessException JavaDoc, InstantiationException JavaDoc
173   {
174     log.fine("Config loading library " + cl.getName());
175
176     for (Method JavaDoc method : cl.getMethods()) {
177       if (! Modifier.isPublic(method.getModifiers()))
178         continue;
179       
180       if (! Modifier.isStatic(method.getModifiers()))
181         continue;
182
183       _methodMap.put(method.getName(), method);
184     }
185   }
186 }
187
Popular Tags