org.azkfw.business.logic.LogicManager.java Source code

Java tutorial

Introduction

Here is the source code for org.azkfw.business.logic.LogicManager.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.azkfw.business.logic;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.digester3.Digester;
import org.azkfw.business.BusinessServiceException;
import org.azkfw.business.manager.AbstractManager;
import org.azkfw.business.property.PropertyFile;
import org.azkfw.context.Context;
import org.azkfw.util.StringUtility;
import org.xml.sax.SAXException;

/**
 * ??????????
 * 
 * @since 1.0.0
 * @version 1.0.0 2012/09/21
 * @author Kawakicchi
 */
public final class LogicManager extends AbstractManager {

    /**
     * Instance
     */
    private static final LogicManager INSTANCE = new LogicManager();

    /**
     * logics
     */
    private Map<String, Map<String, LogicData>> logics = new HashMap<String, Map<String, LogicData>>();

    /**
     * 
     * <p>
     * ???
     * </p>
     */
    private LogicManager() {
        super(LogicManager.class);
    }

    /**
     * ????
     */
    public synchronized static void initialize() {
        INSTANCE.doInitialize();
    }

    /**
     * ??
     */
    public synchronized static void destroy() {
        INSTANCE.doDestroy();
    }

    /**
     * ?
     * 
     * @param aStream 
     * @param aContext 
     * @throws BusinessServiceException ????????
     * @throws IOException ????????
     */
    public synchronized static void load(final InputStream aStream, final Context aContext)
            throws BusinessServiceException, IOException {
        INSTANCE.doLoad(StringUtility.EMPTY, aStream, aContext);
    }

    /**
     * ?
     * 
     * @param aNamespace ???
     * @param aStream 
     * @param aContext 
     * @throws BusinessServiceException ????????
     * @throws IOException ????????
     */
    public synchronized static void load(final String aNamespace, final InputStream aStream, final Context aContext)
            throws BusinessServiceException, IOException {
        INSTANCE.doLoad(StringUtility.EMPTY, aStream, aContext);
    }

    /**
     * ??
     * 
     * @param aNamespace ???
     * @param aName ??
     * @return ??????<code>null</code>?
     * @throws BusinessServiceException ????????
     */
    public static Logic create(final String aNamespace, final String aName) throws BusinessServiceException {
        return INSTANCE.doCreate(aNamespace, aName);
    }

    /**
     * ??
     * 
     * @param aName ??
     * @return ??????<code>null</code>?
     * @throws BusinessServiceException ????????
     */
    public static Logic create(final String aName) throws BusinessServiceException {
        return INSTANCE.doCreate(StringUtility.EMPTY, aName);
    }

    /**
     * ???
     */
    private void doInitialize() {

    }

    /**
     * ??
     */
    private void doDestroy() {

    }

    /**
     * ?
     * 
     * @param aNamespace ???
     * @param aStream 
     * @param aContext 
     * @throws BusinessServiceException ????????
     * @throws IOException ????????
     */
    @SuppressWarnings("unchecked")
    private void doLoad(final String aNamespace, final InputStream aStream, final Context aContext)
            throws BusinessServiceException, IOException {

        List<LogicEntity> logicList = null;
        try {
            Digester digester = new Digester();
            digester.addObjectCreate("azuki/logics", ArrayList.class);
            digester.addObjectCreate("azuki/logics/logic", LogicEntity.class);
            digester.addSetProperties("azuki/logics/logic");
            digester.addSetNext("azuki/logics/logic", "add");
            logicList = digester.parse(aStream);
        } catch (SAXException ex) {
            error(ex);
            throw new IOException(ex);
        } catch (IOException ex) {
            error(ex);
            throw new IOException(ex);
        }

        Map<String, LogicData> m = null;
        if (logics.containsKey(aNamespace)) {
            m = logics.get(aNamespace);
        } else {
            m = new HashMap<String, LogicData>();
        }

        for (int i = 0; i < logicList.size(); i++) {
            LogicEntity logic = logicList.get(i);
            info("Logic loading.[" + logic.name + "]");
            if (m.containsKey(logic.getName())) {
                throw new BusinessServiceException("Duplicate logic name.[" + logic.getName() + "]");
            } else {
                try {
                    LogicData data = new LogicData();

                    Class<Logic> clazz = (Class<Logic>) Class.forName(logic.getLogic());

                    // XXX ????????
                    Map<String, Object> properties = new HashMap<String, Object>();
                    PropertyFile propertyFile = clazz.getAnnotation(PropertyFile.class);
                    if (null != propertyFile) {
                        String property = propertyFile.value();
                        if (StringUtility.isNotEmpty(property)) {
                            InputStream is = aContext.getResourceAsStream(property);
                            if (null != is) {
                                Properties p = new Properties();
                                p.load(is);
                                for (String key : p.stringPropertyNames()) {
                                    properties.put(key, p.getProperty(key));
                                }
                            } else {
                                throw new BusinessServiceException(
                                        "Not found logic property file.[" + property + "]");
                            }
                        }
                    }
                    data.setLogic(clazz);
                    data.setProperties(properties);
                    data.setEntity(logic);
                    m.put(logic.getName(), data);
                } catch (ClassNotFoundException ex) {
                    error(ex);
                    throw new BusinessServiceException(ex);
                }
            }
        }
        logics.put(aNamespace, m);
    }

    /**
     * ??
     * 
     * @param aNamespace ???
     * @param aName ??
     * @return ??????<code>null</code>?
     * @throws BusinessServiceException ????????
     */
    private Logic doCreate(final String aNamespace, final String aName) throws BusinessServiceException {
        Logic logic = null;
        try {
            if (logics.containsKey(aNamespace)) {
                Map<String, LogicData> ls = logics.get(aNamespace);
                if (ls.containsKey(aName)) {
                    LogicData d = ls.get(aName);
                    logic = d.getLogic().newInstance();
                }
            }
        } catch (InstantiationException ex) {
            throw new BusinessServiceException(ex);
        } catch (IllegalAccessException ex) {
            throw new BusinessServiceException(ex);
        }
        return logic;
    }

    private static class LogicData {

        private Class<Logic> logic;

        private Map<String, Object> properties;

        @SuppressWarnings("unused")
        private LogicEntity entity;

        public void setLogic(final Class<Logic> aLogic) {
            logic = aLogic;
        }

        public void setProperties(final Map<String, Object> aProperties) {
            properties = new HashMap<String, Object>(aProperties);
        }

        public void setEntity(final LogicEntity aEntity) {
            entity = aEntity;
        }

        public Class<Logic> getLogic() {
            return logic;
        }

        @SuppressWarnings("unused")
        public Map<String, Object> getProperties() {
            return properties;
        }
    }

    /**
     * ?????????
     * 
     * @since 1.0.0
     * @version 1.0.0 12/09/21
     * @author Kawakicchi
     * 
     */
    public static class LogicEntity {

        /**
         * ??
         */
        private String name;

        /**
         * 
         */
        private String clazz;

        /**
         * 
         */
        private String inter;

        /**
         * ???
         * 
         * @param aName ??
         */
        public void setName(final String aName) {
            name = aName;
        }

        /**
         * ?
         * 
         * @param aClass 
         */
        public void setLogic(final String aClass) {
            clazz = aClass;
        }

        /**
         * ?
         * 
         * @param aInterface 
         */
        public void setInterface(final String aInterface) {
            inter = aInterface;
        }

        /**
         * ????
         * 
         * @return ??
         */
        public String getName() {
            return name;
        }

        /**
         * ??
         * 
         * @return 
         */
        public String getLogic() {
            return clazz;
        }

        /**
         * ??
         * 
         * @return 
         */
        public String getInterface() {
            return inter;
        }
    }

}