JNDIFolderFactory.java :  » Inversion-of-Control » carbon » org » sape » carbon » services » config » jndi » Java Open Source

Java Open Source » Inversion of Control » carbon 
carbon » org » sape » carbon » services » config » jndi » JNDIFolderFactory.java
/*
 * The contents of this file are subject to the Sapient Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://carbon.sf.net/License.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is The Carbon Component Framework.
 *
 * The Initial Developer of the Original Code is Sapient Corporation
 *
 * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
 */

package org.sape.carbon.services.config.jndi;

import javax.naming.Name;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;

import org.sape.carbon.core.config.node.Node;
import org.sape.carbon.core.config.node.NodeCreationException;
import org.sape.carbon.core.config.node.NodeFactory;
import org.sape.carbon.core.exception.InvalidParameterException;


/**
 * This class provides the functionality for creating JNDIFolderFactory nodes.
 *
 * <br>Copyright 2003 Sapient
 * @since carbon 2.0
 * @author Douglas Voet, March 2003
 * @version $Revision: 1.7 $($Author: dvoet $ / $Date: 2003/05/05 21:21:10 $)
 */
public class JNDIFolderFactory
    implements NodeFactory {

    private JNDILinkNodeConfiguration config;

    /**
     * Constructs a new factory
     * @param config configuration used to get the names of the attributes
     * that hold node name, node and document type, and document content as well
     * as valid attribute values for node and document type.
     */
    public JNDIFolderFactory(JNDILinkNodeConfiguration config) {
        if (config == null) {
            throw new InvalidParameterException(
                this.getClass(),
                "config cannot be null");
        }

        this.config = config;
    }

    /**
     * Creates JNDIConfigurationDocument objects only as children
     * of JNDIFolders.
     *
     * @param parent the parent node of the folder to be created, must be of
     * type JNDIFolder
     * @param name the name of the node to create
     * @throws InvalidParameterExcpetion if parent is not
     * assignable from JNDIFolder or name is null
     */
    public Node getInstance(Node parent, String name)
        throws NodeCreationException {

        // these constants are defined in order to make the code below readable
        final String NAME = this.config.getNodeNameAttributeName();
        final String EQUALS = this.config.getAttributeNameValueSeparator();
        final String NODE_TYPE = this.config.getNodeTypeAttributeName();
        final String FOLDER = this.config.getFolderNodeTypeAttributeValue();

        JNDIFolder parentJNDIFolder;
        try {
            parentJNDIFolder = (JNDIFolder) parent;
        } catch(ClassCastException cce) {
            throw new InvalidParameterException(
                this.getClass(),
                "parent is not assignable from JNDIFolder", cce);
        }

        if (name == null) {
            throw new InvalidParameterException(
                this.getClass(),
                "name cannot be null");
        }

        try {
            // create the context name for the new node
            Name nodeContextName =
                (Name) parentJNDIFolder.getNodeContextName().clone();
            nodeContextName.add(NAME + EQUALS + name);

            DirContext initialContext = parentJNDIFolder.getInitialContext();

            try {
                // lookup the folder's attributes and validate them
                Attributes folderAttributes = initialContext.getAttributes(
                    nodeContextName,
                    new String[] { NODE_TYPE });

                if (!folderAttributes.get(NODE_TYPE).contains(FOLDER)) {
                    // the backing data was not the correct node type
                    throw new NodeCreationException(
                        this.getClass(),
                        parent,
                        name,
                        "Context ["
                            + nodeContextName.toString()
                            + "] exists, but is missing required folder attribute ["
                            + NODE_TYPE
                            + EQUALS
                            + FOLDER
                            + "]");
                }

            } catch (NameNotFoundException nnfe) {
                // context was not found, so create it
                Attributes folderAttributes =
                    new BasicAttributes(NODE_TYPE, FOLDER);

                folderAttributes.put(NAME, name);

                initialContext.createSubcontext(nodeContextName, folderAttributes);
            }

            // create the new JNDIFolder and return it
            return new JNDIFolder(
                parent,
                name,
                parentJNDIFolder.getSubFolderFactory(),
                parentJNDIFolder.getConfigurationDocumentFactory(),
                parentJNDIFolder.getLinkNodeFactory(),
                initialContext,
                nodeContextName,
                this.config);

        } catch (NamingException ne) {
            throw new NodeCreationException(
                this.getClass(),
                parent,
                name,
                "Problem reading JNDI directory",
                ne);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.