KickJava   Java API By Example, From Geeks To Geeks.

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


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.DirContext JavaDoc;
25
26 import org.sape.carbon.core.config.format.ConfigurationFormatService;
27 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
28 import org.sape.carbon.core.config.node.Node;
29 import org.sape.carbon.core.config.node.NodeCreationException;
30 import org.sape.carbon.core.config.node.NodeFactory;
31 import org.sape.carbon.core.exception.InvalidParameterException;
32
33 /**
34  * This class provides most of the functionality for creating
35  * JNDIConfigurationDocument nodes. Extentions implement the
36  * getTypeAttributeValue method that provides the type of the document (link
37  * or document).
38  *
39  * <br>Copyright 2003 Sapient
40  * @since carbon 2.0
41  * @author Douglas Voet, March 2003
42  * @version $Revision: 1.8 $($Author: dvoet $ / $Date: 2003/05/05 21:21:10 $)
43  */

44 public abstract class AbstractJNDIConfigurationDocumentFactory
45     implements NodeFactory {
46
47     private static final ConfigurationFormatService FORMAT_SERVICE =
48         new DefaultConfigurationFormatService();
49
50     protected JNDILinkNodeConfiguration config;
51
52     /**
53      * Constructs a new factory
54      * @param config configuration used to get the names of the attributes
55      * that hold node name, node and document type, and document content as well
56      * as valid attribute values for node and document type.
57      */

58     public AbstractJNDIConfigurationDocumentFactory(
59         JNDILinkNodeConfiguration config) {
60
61         if (config == null) {
62             throw new InvalidParameterException(
63                 this.getClass(),
64                 "config cannot be null");
65         }
66
67         this.config = config;
68     }
69
70     /**
71      * Creates JNDIConfigurationDocument objects only as children
72      * of JNDIFolders.
73      *
74      * @param parent the parent node of the folder to be created, must be of
75      * type JNDIFolder
76      * @param name the name of the node to create
77      * @throws InvalidParameterException if parent is not
78      * assignable from JNDIFolder or name is null
79      */

80     public Node getInstance(Node parent, String JavaDoc name)
81         throws NodeCreationException {
82
83         // these constants are defined in order to make the code below readable
84
final String JavaDoc NAME = this.config.getNodeNameAttributeName();
85         final String JavaDoc EQUALS = this.config.getAttributeNameValueSeparator();
86         final String JavaDoc NODE_TYPE = this.config.getNodeTypeAttributeName();
87         final String JavaDoc FOLDER = this.config.getFolderNodeTypeAttributeValue();
88         final String JavaDoc DOCUMENT = this.config.getDocumentNodeTypeAttributeValue();
89         final String JavaDoc DOC_TYPE = this.config.getDocumentTypeAttributeName();
90         final String JavaDoc CONTENT = this.config.getDocumentContectAttributeName();
91
92         if (name == null) {
93             throw new InvalidParameterException(
94                 this.getClass(),"name parameter cannot be null");
95         }
96
97         JNDIFolder parentJNDIFolder;
98         try {
99             parentJNDIFolder = (JNDIFolder) parent;
100         } catch(ClassCastException JavaDoc cce) {
101             throw new InvalidParameterException(
102                 this.getClass(),
103                 "parent is not assignable from JNDIFolder", cce);
104         }
105
106         try {
107             // create the context name for the new node
108
Name JavaDoc nodeContextName =
109                 (Name JavaDoc) parentJNDIFolder.getNodeContextName().clone();
110             nodeContextName.add(NAME + EQUALS + name);
111
112             DirContext JavaDoc initialContext = parentJNDIFolder.getInitialContext();
113
114             try {
115                 // lookup the document's attributes and validate them
116
String JavaDoc[] docAttributesIDs = new String JavaDoc[] { NODE_TYPE, DOC_TYPE };
117                 Attributes JavaDoc docAttributes = initialContext.getAttributes(
118                     nodeContextName,
119                     docAttributesIDs);
120
121                 if (!docAttributes.get(NODE_TYPE).contains(DOCUMENT)) {
122                     // the backing data was not the correct node type
123
throw new NodeCreationException(
124                         this.getClass(),parent,name,
125                         "Context ["
126                             + nodeContextName.toString()
127                             + "] exists, but is missing required document attribute: ["
128                             + NODE_TYPE
129                             + EQUALS
130                             + DOCUMENT
131                             + "]");
132                 }
133                 if (!docAttributes.get(DOC_TYPE).contains(getTypeAttributeValue())) {
134                     // the backing data was not the correct document type
135
throw new NodeCreationException(
136                         this.getClass(),parent,name,
137                         "Context ["
138                             + nodeContextName.toString()
139                             + "] exists, but has incorrect type, expected type: ["
140                             + getTypeAttributeValue()
141                             + "], actual type: ["
142                             + docAttributes.get(DOC_TYPE)
143                             + "]");
144                 }
145
146             } catch (NameNotFoundException JavaDoc nnfe) {
147                 // context does not exist yet, but that is ok, it will be
148
// created later
149
}
150
151             // create the new JNDIConfigurationDocument and return it
152
return new JNDIConfigurationDocument(
153                 parent,
154                 name,
155                 getConfigurationFormatService(),
156                 parentJNDIFolder.getInitialContext(),
157                 nodeContextName,
158                 this.config,
159                 getTypeAttributeValue());
160
161         } catch (NamingException JavaDoc ne) {
162             throw new NodeCreationException(
163                 this.getClass(),parent,name,
164                 "Problem reading JNDI directory",
165                 ne);
166         }
167     }
168
169     /**
170      * Gets the value of the type attribute for the node being created by the
171      * factory.
172      * The attribute is used to identify the type of node contained
173      * within the document (ConfigurationDocument or LinkNode at this time).
174      *
175      * @return String the type attribute value
176      */

177     protected abstract String JavaDoc getTypeAttributeValue();
178
179     /**
180      * Method that determines which ConfigurationFormatService will be used
181      * within the ConfigurationDocuments of this package. The default
182      * implementation returns a static instance of
183      * org.sape.carbon.core.config.format.DefaultConfigurationFormatService.
184      * Override this method to
185      * change the ConfigurationFormatService used by the
186      * JNDIConfigurationDocument.
187      *
188      * @return ConfigurationFormatService
189      */

190     protected ConfigurationFormatService getConfigurationFormatService() {
191         return AbstractJNDIConfigurationDocumentFactory.FORMAT_SERVICE;
192     }
193 }
Popular Tags