KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > ResourceRef


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.types;
31
32 import com.caucho.config.BuilderProgram;
33 import com.caucho.config.Config;
34 import com.caucho.config.ConfigException;
35 import com.caucho.config.LineConfigException;
36 import com.caucho.loader.ClassLoaderListener;
37 import com.caucho.loader.DynamicClassLoader;
38 import com.caucho.loader.EnvironmentClassLoader;
39 import com.caucho.naming.Jndi;
40 import com.caucho.util.L10N;
41
42 import javax.annotation.PostConstruct;
43 import javax.naming.InitialContext JavaDoc;
44 import javax.naming.NamingException JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Configuration for the init-param pattern.
52  */

53 public class ResourceRef implements Validator {
54   private static Logger JavaDoc log = Logger.getLogger(ResourceRef.class.getName());
55   private static L10N L = new L10N(ResourceRef.class);
56
57   private String JavaDoc _location = "";
58
59   private String JavaDoc _name;
60   private Class JavaDoc _type;
61   private String JavaDoc _description;
62   private boolean _sharing;
63
64   private BuilderProgram _init;
65   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _params = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
66
67   /**
68    * Sets the id
69    */

70   public void setId(String JavaDoc id)
71   {
72   }
73
74   /**
75    * Sets the configuration location.
76    */

77   public void setConfigLocation(String JavaDoc filename, int line)
78   {
79     _location = filename + ":" + line + " ";
80   }
81
82   /**
83    * Sets the description.
84    */

85   public void setDescription(String JavaDoc description)
86   {
87     _description = description;
88   }
89
90   /**
91    * Sets the name
92    */

93   public void setResRefName(String JavaDoc name)
94   {
95     _name = name;
96   }
97
98   /**
99    * Gets the name
100    */

101   public String JavaDoc getResRefName()
102   {
103     return _name;
104   }
105
106   /**
107    * Sets the type
108    */

109   public void setResType(Class JavaDoc type)
110   {
111     _type = type;
112   }
113
114   /**
115    * Sets the auth
116    */

117   public void setResAuth(String JavaDoc auth)
118   {
119   }
120
121   /**
122    * Sets the sharing scope
123    */

124   public void setResSharingScope(String JavaDoc share)
125   {
126   }
127
128   /**
129    * Sets the type
130    */

131   public void setClassName(Class JavaDoc type)
132   {
133     _type = type;
134   }
135
136   /**
137    * Gets the type;
138    */

139   public Class JavaDoc getResType()
140   {
141     return _type;
142   }
143
144   /**
145    * Sets the init program
146    */

147   public void setInit(BuilderProgram init)
148   {
149     _init = init;
150   }
151
152   /**
153    * Gets the init program;
154    */

155   public BuilderProgram getInit()
156   {
157     return _init;
158   }
159
160   /**
161    * Sets an init-parameter
162    */

163   public void setInitParam(InitParam initParam)
164   {
165     _params.putAll(initParam.getParameters());
166   }
167
168   /**
169    * Initialize the resource.
170    */

171   @PostConstruct
172   public void init()
173     throws Throwable JavaDoc
174   {
175     if (_init == null && _params.size() == 0) {
176       return;
177     }
178     
179     Class JavaDoc cl = _type;
180
181     if (javax.sql.DataSource JavaDoc.class.equals(_type))
182       cl = com.caucho.sql.DBPool.class;
183     /*
184     else if (javax.sql.XADataSource.class.equals(_type))
185       cl = com.caucho.sql.XAPool.class;
186     */

187
188     Object JavaDoc obj = cl.newInstance();
189
190     if (_init != null)
191       _init.configure(obj);
192
193     Iterator JavaDoc iter = _params.keySet().iterator();
194     while (iter.hasNext()) {
195       String JavaDoc key = (String JavaDoc) iter.next();
196       String JavaDoc value = (String JavaDoc) _params.get(key);
197
198       Config.setAttribute(obj, key, value);
199     }
200
201     if (obj instanceof ClassLoaderListener) {
202       ClassLoaderListener listener = (ClassLoaderListener) obj;
203       
204       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
205       for (; loader != null; loader = loader.getParent()) {
206         if (loader instanceof EnvironmentClassLoader) {
207           ((DynamicClassLoader) loader).addListener(listener);
208           break;
209         }
210       }
211     }
212
213     Jndi.bindDeep(_name, obj);
214   }
215
216   /**
217    * Validates the resource-ref, i.e. checking that it exists in
218    * JNDI.
219    */

220   public void validate()
221     throws ConfigException
222   {
223     Object JavaDoc obj = null;
224     
225     try {
226       obj = new InitialContext JavaDoc().lookup("java:comp/env/" + _name);
227     } catch (NamingException JavaDoc e) {
228       log.log(Level.FINEST, e.toString(), e);
229     }
230
231     if (obj == null)
232       throw error(L.l("resource-ref '{0}' was not configured. All resources defined by <resource-ref> tags must be defined in a configuration file.",
233               _name));
234   }
235
236   public ConfigException error(String JavaDoc msg)
237   {
238     if (_location != null)
239       return new LineConfigException(_location + msg);
240     else
241       return new ConfigException(msg);
242   }
243
244   public String JavaDoc toString()
245   {
246     return "ResourceRef[" + _name + "]";
247   }
248 }
249
Popular Tags