KickJava   Java API By Example, From Geeks To Geeks.

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


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.BeanBuilderException;
32 import com.caucho.loader.ClassLoaderListener;
33 import com.caucho.loader.Environment;
34 import com.caucho.loader.EnvironmentListener;
35 import com.caucho.naming.Jndi;
36 import com.caucho.naming.LinkProxy;
37 import com.caucho.util.L10N;
38
39 import javax.annotation.PostConstruct;
40 import javax.naming.spi.InitialContextFactory JavaDoc;
41 import java.util.Hashtable JavaDoc;
42
43 /**
44  * Configuration for the init-param pattern.
45  */

46 public class JndiLink {
47   private static L10N L = new L10N(JndiLink.class);
48
49   private String JavaDoc _name;
50   private Class JavaDoc _factoryClass;
51   private String JavaDoc _description;
52
53   private Hashtable JavaDoc _properties = new Hashtable JavaDoc();
54
55   private InitProgram _init;
56
57   /**
58    * Sets the name
59    */

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

68   public String JavaDoc getName()
69   {
70     return _name;
71   }
72
73   /**
74    * Sets the factory
75    */

76   public void setFactory(Class JavaDoc factory)
77   {
78     _factoryClass = factory;
79   }
80
81   /**
82    * Gets the type;
83    */

84   public Class JavaDoc getFactory()
85   {
86     return _factoryClass;
87   }
88
89   /**
90    * Sets the init program
91    */

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

100   public InitProgram getInit()
101   {
102     return _init;
103   }
104
105   /**
106    * Initialize the resource.
107    */

108   @PostConstruct
109   public void init()
110     throws Exception JavaDoc
111   {
112     if (_name == null)
113       throw new BeanBuilderException(L.l("<jndi-link> configuration needs a <name>. The <name> is the JNDI name where the context will be linked."));
114     
115     Class JavaDoc factory = _factoryClass;
116
117     if (factory == null)
118       throw new BeanBuilderException(L.l("<jndi-link> configuration need a <factory>. The <factory> is the class name of the InitialContextFactory bean."));
119
120     Object JavaDoc obj = factory.newInstance();
121
122     /*
123     configure(obj);
124
125     TypeBuilderFactory.init(obj);
126     */

127
128     if (obj instanceof ClassLoaderListener) {
129       ClassLoaderListener listener = (ClassLoaderListener) obj;
130
131       Environment.addClassLoaderListener(listener);
132     }
133
134     if (obj instanceof EnvironmentListener) {
135       EnvironmentListener listener = (EnvironmentListener) obj;
136
137       Environment.addEnvironmentListener(listener);
138     }
139
140     Object JavaDoc proxy = new LinkProxy((InitialContextFactory JavaDoc) obj,
141                                  _properties,
142                                  null);
143
144     if (_name.startsWith("java:comp"))
145       Jndi.bindDeep(_name, proxy);
146     else {
147       Jndi.bindDeep("java:comp/env/" + _name, proxy);
148     }
149   }
150
151   protected void configure(Object JavaDoc obj)
152     throws Throwable JavaDoc
153   {
154     if (_init != null)
155       _init.init(obj);
156   }
157
158   public String JavaDoc toString()
159   {
160     return "JndiLink[" + _name + "]";
161   }
162 }
163
164
Popular Tags