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

Java Open Source » Inversion of Control » carbon 
carbon » org » sape » carbon » services » config » jar » JarLinkNodeFactory.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.jar;

import java.io.File;

import org.sape.carbon.core.config.InvalidConfigurationException;
import org.sape.carbon.core.config.format.ConfigurationFormatException;
import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
import org.sape.carbon.core.config.node.ConfigurationDocument;
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.config.node.NodeIOException;
import org.sape.carbon.core.config.node.link.ConfigurationDocumentLinkImpl;
import org.sape.carbon.core.config.node.link.FolderLinkImpl;
import org.sape.carbon.core.config.node.link.LinkNode;
import org.sape.carbon.core.config.node.link.LinkNodeFactory;
import org.sape.carbon.core.util.jar.EnhancedJarFile;
import org.sape.carbon.core.util.thread.ReadWriteLock;
import org.sape.carbon.core.util.thread.WriterPreferenceReadWriteLock;

/**
 *
 * Copyright 2002 Sapient
 * @since carbon 1.0
 * @author Douglas Voet, April 2002
 * @version $Revision: 1.2 $($Author: dvoet $ / $Date: 2003/05/05 21:21:09 $)
 */
public class JarLinkNodeFactory implements LinkNodeFactory {

    /** Holds a reference to a JarFolderFactory */
    private static final NodeFactory subFolderFactory =
        new JarFolderFactory();

    /** Holds a reference to a JarConfigurationDocumentFactory */
    private static final NodeFactory configurationDocumentFactory =
        new JarConfigurationDocumentFactory();

    /** Holds a reference to a JarLinkNodeConfigurationDocumentFactory */
    private static final NodeFactory linkNodeFactory =
        new JarLinkNodeConfigurationDocumentFactory();

    /**
     * @see LinkNodeFactory#getInstance
     */
    public LinkNode getInstance(Node parent, String name,
        ConfigurationDocument linkConfigurationDoc)
        throws NodeCreationException {

        try {
            JarLinkNodeConfiguration jarLinkConfiguration =
                (JarLinkNodeConfiguration)
                    linkConfigurationDoc.readConfiguration();

            File targetJarFile =
                new File(getTargetPath(jarLinkConfiguration));

            if (!targetJarFile.exists() || targetJarFile.isDirectory()) {
                throw new NodeCreationException(
                    this.getClass(),
                    parent,
                    name,
                    "Target of link does not exist or is directory");
            }

            String targetWithinJar =
                jarLinkConfiguration.getTargetRootWithinJar();

            ReadWriteLock readWriteLock;

            if (parent instanceof JarFolder
                && ((JarFolder) parent).getInternalJarFile().
                    equals(targetJarFile)) {

                if (targetWithinJar == null) {
                    throw new InvalidConfigurationException(
                        this.getClass(),
                        linkConfigurationDoc.getName(),
                        "TargetRootWithinJar",
                        "Link is relative, but TargetRootWithinJar "
                            + "is not specified");
                }

                JarFolder parentJar = (JarFolder) parent;
                readWriteLock = parentJar.getReadWriteLock();
                targetWithinJar =
                    parentJar.getInternalJarEntryName() + targetWithinJar;

            } else {
                // this is a new jar an gets its own monitor
                readWriteLock = new WriterPreferenceReadWriteLock();
            }

            if (targetWithinJar == null
               || targetWithinJar.equals("")
               || targetWithinJar.endsWith(EnhancedJarFile.JAR_DELIMETER)) {

                JarFolder target = new JarFolder(parent, name,
                    getSubFolderFactory(),
                    getConfigurationDocumentFactory(),
                    getLinkNodeFactory(),
                    readWriteLock,
                    targetJarFile,
                    targetWithinJar);

                if (!target.backingDataExists()) {
                    throw new NodeCreationException(
                        this.getClass(),
                        parent,
                        name,
                        "Entry name ["
                            + targetWithinJar
                            + "] does not exist in jar ["
                            + targetJarFile.getAbsolutePath()
                            + "]");
                }

                return new FolderLinkImpl(linkConfigurationDoc, target);

            } else {
                JarConfigurationDocument target = new JarConfigurationDocument(
                    parent,
                    name,
                    new DefaultConfigurationFormatService(),
                    readWriteLock,
                    targetJarFile,
                    targetWithinJar);

                if (!target.backingDataExists()) {
                    throw new NodeCreationException(
                        this.getClass(),
                        parent,
                        name,
                        "Entry name ["
                            + targetWithinJar
                            + "] does not exist in jar ["
                            + targetJarFile.getAbsolutePath()
                            + "]");
                }

                return new ConfigurationDocumentLinkImpl(
                    linkConfigurationDoc,
                    target);
            }

        } catch (ClassCastException cce) {
            throw new InvalidConfigurationException(
                this.getClass(),
                linkConfigurationDoc.getName(),
                "LinkNodeFactory",
                "LinkNodeFactory is not an instance of "
                    + JarLinkNodeConfiguration.class.getName(), cce);

        } catch (NodeIOException nioe) {
            throw new InvalidConfigurationException(
                this.getClass(),
                linkConfigurationDoc.getName(), null,
                "Could not read link configuration document", nioe);

        } catch (ConfigurationFormatException cfe) {
            throw new InvalidConfigurationException(
                this.getClass(),
                linkConfigurationDoc.getName(), null,
                "Could not read link configuration document", cfe);
        }
    }

    /**
     * Gets the factory used within this package to create folders
     *
     * @return NodeFactory
     */
    protected NodeFactory getSubFolderFactory() {
        return JarLinkNodeFactory.subFolderFactory;
    }

    /**
     * Gets the factory used within this package to create
     * configuration documents
     *
     * @return NodeFactory
     */
    protected NodeFactory getConfigurationDocumentFactory() {
        return JarLinkNodeFactory.configurationDocumentFactory;
    }

    /**
     * Gets the factory used within this package to create links
     *
     * @return NodeFactory
     */
    protected NodeFactory getLinkNodeFactory() {
        return JarLinkNodeFactory.linkNodeFactory;
    }

    /**
     * Gets and validates the target jar path from configuration
     *
     * @param jarLinkConfiguration the link config to pull the target
     *        jar path from
     * @return String the target jar path from configuration
     */
    private String getTargetPath(
        JarLinkNodeConfiguration jarLinkConfiguration) {

        String targetJarPath = jarLinkConfiguration.getTargetJarPath();
        if (targetJarPath == null) {
            throw new InvalidConfigurationException(
                this.getClass(),
                jarLinkConfiguration.getConfigurationName(),
                "TargetJarPath");
        }

        return targetJarPath;
    }
}
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.