001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.config.jndi;
019:
020: import javax.naming.Name;
021: import javax.naming.NameNotFoundException;
022: import javax.naming.NamingException;
023: import javax.naming.directory.Attributes;
024: import javax.naming.directory.BasicAttributes;
025: import javax.naming.directory.DirContext;
026:
027: import org.sape.carbon.core.config.node.Node;
028: import org.sape.carbon.core.config.node.NodeCreationException;
029: import org.sape.carbon.core.config.node.NodeFactory;
030: import org.sape.carbon.core.exception.InvalidParameterException;
031:
032: /**
033: * This class provides the functionality for creating JNDIFolderFactory nodes.
034: *
035: * <br>Copyright 2003 Sapient
036: * @since carbon 2.0
037: * @author Douglas Voet, March 2003
038: * @version $Revision: 1.7 $($Author: dvoet $ / $Date: 2003/05/05 21:21:10 $)
039: */
040: public class JNDIFolderFactory implements NodeFactory {
041:
042: private JNDILinkNodeConfiguration config;
043:
044: /**
045: * Constructs a new factory
046: * @param config configuration used to get the names of the attributes
047: * that hold node name, node and document type, and document content as well
048: * as valid attribute values for node and document type.
049: */
050: public JNDIFolderFactory(JNDILinkNodeConfiguration config) {
051: if (config == null) {
052: throw new InvalidParameterException(this .getClass(),
053: "config cannot be null");
054: }
055:
056: this .config = config;
057: }
058:
059: /**
060: * Creates JNDIConfigurationDocument objects only as children
061: * of JNDIFolders.
062: *
063: * @param parent the parent node of the folder to be created, must be of
064: * type JNDIFolder
065: * @param name the name of the node to create
066: * @throws InvalidParameterExcpetion if parent is not
067: * assignable from JNDIFolder or name is null
068: */
069: public Node getInstance(Node parent, String name)
070: throws NodeCreationException {
071:
072: // these constants are defined in order to make the code below readable
073: final String NAME = this .config.getNodeNameAttributeName();
074: final String EQUALS = this .config
075: .getAttributeNameValueSeparator();
076: final String NODE_TYPE = this .config.getNodeTypeAttributeName();
077: final String FOLDER = this .config
078: .getFolderNodeTypeAttributeValue();
079:
080: JNDIFolder parentJNDIFolder;
081: try {
082: parentJNDIFolder = (JNDIFolder) parent;
083: } catch (ClassCastException cce) {
084: throw new InvalidParameterException(this .getClass(),
085: "parent is not assignable from JNDIFolder", cce);
086: }
087:
088: if (name == null) {
089: throw new InvalidParameterException(this .getClass(),
090: "name cannot be null");
091: }
092:
093: try {
094: // create the context name for the new node
095: Name nodeContextName = (Name) parentJNDIFolder
096: .getNodeContextName().clone();
097: nodeContextName.add(NAME + EQUALS + name);
098:
099: DirContext initialContext = parentJNDIFolder
100: .getInitialContext();
101:
102: try {
103: // lookup the folder's attributes and validate them
104: Attributes folderAttributes = initialContext
105: .getAttributes(nodeContextName,
106: new String[] { NODE_TYPE });
107:
108: if (!folderAttributes.get(NODE_TYPE).contains(FOLDER)) {
109: // the backing data was not the correct node type
110: throw new NodeCreationException(
111: this .getClass(),
112: parent,
113: name,
114: "Context ["
115: + nodeContextName.toString()
116: + "] exists, but is missing required folder attribute ["
117: + NODE_TYPE + EQUALS + FOLDER + "]");
118: }
119:
120: } catch (NameNotFoundException nnfe) {
121: // context was not found, so create it
122: Attributes folderAttributes = new BasicAttributes(
123: NODE_TYPE, FOLDER);
124:
125: folderAttributes.put(NAME, name);
126:
127: initialContext.createSubcontext(nodeContextName,
128: folderAttributes);
129: }
130:
131: // create the new JNDIFolder and return it
132: return new JNDIFolder(parent, name, parentJNDIFolder
133: .getSubFolderFactory(), parentJNDIFolder
134: .getConfigurationDocumentFactory(),
135: parentJNDIFolder.getLinkNodeFactory(),
136: initialContext, nodeContextName, this .config);
137:
138: } catch (NamingException ne) {
139: throw new NodeCreationException(this .getClass(), parent,
140: name, "Problem reading JNDI directory", ne);
141: }
142: }
143: }
|