KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.naming.Name JavaDoc;
21 import javax.naming.NameNotFoundException JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.directory.Attributes JavaDoc;
24 import javax.naming.directory.BasicAttributes JavaDoc;
25 import javax.naming.directory.DirContext JavaDoc;
26
27 import org.sape.carbon.core.config.node.Node;
28 import org.sape.carbon.core.config.node.NodeCreationException;
29 import org.sape.carbon.core.config.node.NodeFactory;
30 import org.sape.carbon.core.exception.InvalidParameterException;
31
32
33 /**
34  * This class provides the functionality for creating JNDIFolderFactory nodes.
35  *
36  * <br>Copyright 2003 Sapient
37  * @since carbon 2.0
38  * @author Douglas Voet, March 2003
39  * @version $Revision: 1.7 $($Author: dvoet $ / $Date: 2003/05/05 21:21:10 $)
40  */

41 public class JNDIFolderFactory
42     implements NodeFactory {
43
44     private JNDILinkNodeConfiguration config;
45
46     /**
47      * Constructs a new factory
48      * @param config configuration used to get the names of the attributes
49      * that hold node name, node and document type, and document content as well
50      * as valid attribute values for node and document type.
51      */

52     public JNDIFolderFactory(JNDILinkNodeConfiguration config) {
53         if (config == null) {
54             throw new InvalidParameterException(
55                 this.getClass(),
56                 "config cannot be null");
57         }
58
59         this.config = config;
60     }
61
62     /**
63      * Creates JNDIConfigurationDocument objects only as children
64      * of JNDIFolders.
65      *
66      * @param parent the parent node of the folder to be created, must be of
67      * type JNDIFolder
68      * @param name the name of the node to create
69      * @throws InvalidParameterExcpetion if parent is not
70      * assignable from JNDIFolder or name is null
71      */

72     public Node getInstance(Node parent, String JavaDoc name)
73         throws NodeCreationException {
74
75         // these constants are defined in order to make the code below readable
76
final String JavaDoc NAME = this.config.getNodeNameAttributeName();
77         final String JavaDoc EQUALS = this.config.getAttributeNameValueSeparator();
78         final String JavaDoc NODE_TYPE = this.config.getNodeTypeAttributeName();
79         final String JavaDoc FOLDER = this.config.getFolderNodeTypeAttributeValue();
80
81         JNDIFolder parentJNDIFolder;
82         try {
83             parentJNDIFolder = (JNDIFolder) parent;
84         } catch(ClassCastException JavaDoc cce) {
85             throw new InvalidParameterException(
86                 this.getClass(),
87                 "parent is not assignable from JNDIFolder", cce);
88         }
89
90         if (name == null) {
91             throw new InvalidParameterException(
92                 this.getClass(),
93                 "name cannot be null");
94         }
95
96         try {
97             // create the context name for the new node
98
Name JavaDoc nodeContextName =
99                 (Name JavaDoc) parentJNDIFolder.getNodeContextName().clone();
100             nodeContextName.add(NAME + EQUALS + name);
101
102             DirContext JavaDoc initialContext = parentJNDIFolder.getInitialContext();
103
104             try {
105                 // lookup the folder's attributes and validate them
106
Attributes JavaDoc folderAttributes = initialContext.getAttributes(
107                     nodeContextName,
108                     new String JavaDoc[] { NODE_TYPE });
109
110                 if (!folderAttributes.get(NODE_TYPE).contains(FOLDER)) {
111                     // the backing data was not the correct node type
112
throw new NodeCreationException(
113                         this.getClass(),
114                         parent,
115                         name,
116                         "Context ["
117                             + nodeContextName.toString()
118                             + "] exists, but is missing required folder attribute ["
119                             + NODE_TYPE
120                             + EQUALS
121                             + FOLDER
122                             + "]");
123                 }
124
125             } catch (NameNotFoundException JavaDoc nnfe) {
126                 // context was not found, so create it
127
Attributes JavaDoc folderAttributes =
128                     new BasicAttributes JavaDoc(NODE_TYPE, FOLDER);
129
130                 folderAttributes.put(NAME, name);
131
132                 initialContext.createSubcontext(nodeContextName, folderAttributes);
133             }
134
135             // create the new JNDIFolder and return it
136
return new JNDIFolder(
137                 parent,
138                 name,
139                 parentJNDIFolder.getSubFolderFactory(),
140                 parentJNDIFolder.getConfigurationDocumentFactory(),
141                 parentJNDIFolder.getLinkNodeFactory(),
142                 initialContext,
143                 nodeContextName,
144                 this.config);
145
146         } catch (NamingException JavaDoc ne) {
147             throw new NodeCreationException(
148                 this.getClass(),
149                 parent,
150                 name,
151                 "Problem reading JNDI directory",
152                 ne);
153         }
154     }
155 }
Popular Tags