KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > jndi > JNDILinkNodeFactory


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.config.jndi;
19
20 import java.util.Hashtable JavaDoc;
21
22 import javax.naming.Context JavaDoc;
23 import javax.naming.Name JavaDoc;
24 import javax.naming.NameParser JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.naming.directory.Attribute JavaDoc;
27 import javax.naming.directory.Attributes JavaDoc;
28 import javax.naming.directory.DirContext JavaDoc;
29 import javax.naming.directory.InitialDirContext JavaDoc;
30
31 import org.sape.carbon.core.config.InvalidConfigurationException;
32 import org.sape.carbon.core.config.format.ConfigurationFormatException;
33 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
34 import org.sape.carbon.core.config.node.ConfigurationDocument;
35 import org.sape.carbon.core.config.node.Node;
36 import org.sape.carbon.core.config.node.NodeCreationException;
37 import org.sape.carbon.core.config.node.NodeFactory;
38 import org.sape.carbon.core.config.node.NodeIOException;
39 import org.sape.carbon.core.config.node.link.ConfigurationDocumentLinkImpl;
40 import org.sape.carbon.core.config.node.link.FolderLinkImpl;
41 import org.sape.carbon.core.config.node.link.LinkNode;
42 import org.sape.carbon.core.config.node.link.LinkNodeFactory;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47
48 /**
49  * Factory used to create links to nodes on a JNDI directory.
50  *
51  * Copyright 2003 Sapient
52  * @since carbon 2.0
53  * @author Douglas Voet, February 2003
54  * @version $Revision: 1.10 $($Author: dvoet $ / $Date: 2003/10/30 19:29:58 $)
55  */

56 public class JNDILinkNodeFactory implements LinkNodeFactory {
57
58     private Log log = LogFactory.getLog(this.getClass());
59
60     /**
61      * @inherit
62      */

63     public LinkNode getInstance(Node parent, String JavaDoc name,
64         ConfigurationDocument linkConfigurationDoc)
65         throws NodeCreationException {
66
67         try {
68             JNDILinkNodeConfiguration config = (JNDILinkNodeConfiguration)
69                 linkConfigurationDoc.readConfiguration();
70
71             validateConfig(config);
72
73             // create initial context
74
DirContext JavaDoc initialContext = (DirContext JavaDoc)
75                 config.getInitialContextFactory().getDirContext();
76
77             // get name
78
NameParser JavaDoc nameParser = initialContext.getNameParser("");
79
80             Name JavaDoc initialContextName =
81                 nameParser.parse(config.getTargetContextName());
82
83             // lookup context and get attributes, determine if folder or doc
84
Attributes JavaDoc targetAttributes =
85                 initialContext.getAttributes(
86                     initialContextName,
87                     new String JavaDoc[] { config.getNodeTypeAttributeName() });
88
89             Attribute JavaDoc typeID =
90                 targetAttributes.get(config.getNodeTypeAttributeName());
91
92             // create folder or doc link impl and return
93
if (typeID.contains(config.getFolderNodeTypeAttributeValue())) {
94                 return new FolderLinkImpl(
95                     linkConfigurationDoc,
96                     new JNDIFolder(
97                         parent,
98                         name,
99                         getSubFolderFactory(config),
100                         getConfigurationDocumentFactory(config),
101                         getLinkNodeFactory(config),
102                         initialContext,
103                         initialContextName,
104                         config));
105             }
106
107             if (typeID.contains(config.getDocumentNodeTypeAttributeValue())) {
108                 return new ConfigurationDocumentLinkImpl(
109                     linkConfigurationDoc,
110                     new JNDIConfigurationDocument(
111                         parent,
112                         name,
113                         new DefaultConfigurationFormatService(),
114                         initialContext,
115                         initialContextName,
116                         config,
117                         config.getLinkDocumentTypeAttributeValue()));
118             }
119
120             throw new NodeCreationException(
121                 this.getClass(),
122                 parent,
123                 name,
124                 "InitialContextName ["
125                     + config.getTargetContextName()
126                     + "] did not refer to a document or folder");
127
128         } catch (NodeIOException nioe) {
129             throw new NodeCreationException(
130                 this.getClass(),
131                 parent,
132                 name,
133                 "Could not read link configuration",
134                 nioe);
135
136         } catch (ConfigurationFormatException cfe) {
137             throw new NodeCreationException(
138                 this.getClass(),
139                 parent,
140                 name,
141                 "Could not read link configuration",
142                 cfe);
143
144         } catch (ClassCastException JavaDoc cce) {
145             throw new NodeCreationException(
146                 this.getClass(),
147                 parent,
148                 name,
149                 "Link configuration was not of type "
150                     + JNDILinkNodeConfiguration.class.getName(),
151                 cce);
152
153         } catch (NamingException JavaDoc ne) {
154             throw new NodeCreationException(
155                 this.getClass(),
156                 parent,
157                 name,
158                 "Problem reading JNDI directory",
159                 ne);
160         }
161     }
162
163     /**
164      * Gets the factory used within this package to create folders
165      *
166      * @return NodeFactory
167      */

168     protected NodeFactory getSubFolderFactory(JNDILinkNodeConfiguration config) {
169         return new JNDIFolderFactory(config);
170     }
171
172     /**
173      * Gets the factory used within this package to create
174      * configuration documents
175      *
176      * @return NodeFactory
177      */

178     protected NodeFactory getConfigurationDocumentFactory(JNDILinkNodeConfiguration config) {
179         return new JNDIConfigurationDocumentFactory(config);
180     }
181
182     /**
183      * Gets the factory used within this package to create links
184      *
185      * @return NodeFactory
186      */

187     protected NodeFactory getLinkNodeFactory(JNDILinkNodeConfiguration config) {
188         return new JNDILinkNodeConfigurationDocumentFactory(config);
189     }
190
191     /**
192      * Validates that config has all required properties.
193      * @param config
194      */

195     private void validateConfig(JNDILinkNodeConfiguration config) {
196         if (config.getInitialContextFactory() == null) {
197             throw new InvalidConfigurationException(
198                 this.getClass(),
199                 config.getConfigurationName(),
200                 "InitialContextFactory",
201                 "InitialContextFactory must be specified");
202         }
203
204         if (config.getTargetContextName() == null ||
205             config.getTargetContextName().equals("")) {
206             throw new InvalidConfigurationException(
207                 this.getClass(),
208                 config.getConfigurationName(),
209                 "InitialContextName",
210                 "InitialContextName must be specified");
211         }
212     }
213
214 }
Popular Tags