KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.config.types;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.naming.Jndi;
33 import com.caucho.util.L10N;
34
35 import javax.annotation.PostConstruct;
36 import javax.naming.Reference JavaDoc;
37 import javax.naming.StringRefAddr JavaDoc;
38 import javax.naming.spi.ObjectFactory JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collections JavaDoc;
41 import java.util.HashMap JavaDoc;
42
43 /**
44  * Configuration for the init-param pattern.
45  */

46 public class ReferenceConfig {
47   private static L10N L = new L10N(ReferenceConfig.class);
48
49   private String JavaDoc _name;
50   private Class JavaDoc _factory;
51   private String JavaDoc _description;
52
53   private InitProgram _init;
54   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _params;
55   
56   private ObjectFactory JavaDoc _objectFactory;
57
58   /**
59    * Sets the name
60    */

61   public void setJndiName(String JavaDoc name)
62   {
63     _name = name;
64   }
65
66   /**
67    * Gets the name
68    */

69   public String JavaDoc getJndiName()
70   {
71     return _name;
72   }
73
74   /**
75    * Gets the object factory;
76    */

77   public Class JavaDoc getFactory()
78   {
79     return _factory;
80   }
81
82   /**
83    * Sets the object factory;
84    */

85   public void setFactory(Class JavaDoc factory)
86   {
87     _factory = factory;
88   }
89
90   /**
91    * Sets the init program
92    */

93   public void setInit(InitProgram init)
94   {
95     _init = init;
96   }
97
98   /**
99    * Gets the init program;
100    */

101   public InitProgram getInit()
102   {
103     return _init;
104   }
105
106   /**
107    * Sets an init param.
108    */

109   public void addInitParam(InitParam initParam)
110   {
111     if (_params == null)
112       _params = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
113     
114     _params.putAll(initParam.getParameters());
115   }
116
117   /**
118    * Initialize the resource.
119    */

120   @PostConstruct
121   public void init()
122     throws Exception JavaDoc
123   {
124     Object JavaDoc obj = null;
125     
126     if (_factory == null) {
127       throw new ConfigException(L.l("<reference> configuration need a <factory>. The <factory> is the class name of the resource bean."));
128     }
129     else if (ObjectFactory JavaDoc.class.isAssignableFrom(_factory)) {
130       Reference JavaDoc ref;
131
132       if (_init != null) {
133         throw new ConfigException(L.l("<init> is not allowed for object factories. A <resource> with a <factory> must only have <init-param> configuration."));
134       }
135
136       String JavaDoc factoryName = _factory.getName();
137       ref = new Reference JavaDoc(factoryName, factoryName, null);
138
139       if (_params != null) {
140         ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>(_params.keySet());
141         Collections.sort(names);
142
143         for (int i = 0; i < names.size(); i++) {
144           String JavaDoc name = names.get(i);
145           String JavaDoc value = _params.get(name);
146
147           ref.add(new StringRefAddr JavaDoc(name, value));
148         }
149       }
150
151       obj = ref;
152     }
153     else {
154       throw new ConfigException(L.l("`{0}' must implement ObjectFactory. <factory> classes in <resource> must implement ObjectFactory.", _factory.getName()));
155     }
156
157     if (_name.startsWith("java:comp"))
158       Jndi.bindDeep(_name, obj);
159     else
160       Jndi.bindDeep("java:comp/env/" + _name, obj);
161   }
162
163   protected void configure(Object JavaDoc obj)
164     throws Throwable JavaDoc
165   {
166     if (_init != null)
167       _init.init(obj);
168   }
169
170   public String JavaDoc toString()
171   {
172     return "Resource[" + _name + "]";
173   }
174 }
175
176
Popular Tags