001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.jmsmanager.handlers;
019    
020    import java.io.File;
021    import java.io.IOException;
022    import java.util.List;
023    
024    import javax.jms.Queue;
025    import javax.jms.Topic;
026    import javax.management.ObjectName;
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletException;
030    
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.geronimo.console.core.jms.TopicBrowserGBean;
034    import org.apache.geronimo.console.jmsmanager.AbstractJMSManager;
035    import org.apache.geronimo.gbean.AbstractName;
036    import org.apache.geronimo.gbean.GBeanData;
037    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
038    import org.apache.geronimo.kernel.config.ConfigurationData;
039    import org.apache.geronimo.kernel.config.ConfigurationManager;
040    import org.apache.geronimo.kernel.config.ConfigurationUtil;
041    import org.apache.geronimo.kernel.repository.Artifact;
042    import org.apache.geronimo.kernel.repository.Dependency;
043    import org.apache.geronimo.kernel.repository.ImportType;
044    
045    public class CreateDestinationHandler extends AbstractJMSManager implements PortletResponseHandler {
046        protected static Log log = LogFactory
047                .getLog(CreateDestinationHandler.class);
048    
049        private static final Artifact parentId = new Artifact("geronimo", "activemq-broker", org.apache.geronimo.system.serverinfo.ServerConstants.getVersion(), "car");
050    
051    //    static final GBeanInfo QUEUE_INFO;
052    //
053    //    static final GBeanInfo TOPIC_INFO;
054    //
055    //    static {
056    //        GBeanInfoBuilder queueInfoBuilder = new GBeanInfoBuilder(
057    //                AdminObjectWrapper.class, AdminObjectWrapperGBean.GBEAN_INFO);
058    //        queueInfoBuilder.addAttribute(new DynamicGAttributeInfo("PhysicalName",
059    //                String.class.getName(), true, true, true, true));
060    //        QUEUE_INFO = queueInfoBuilder.getBeanInfo();
061    //        GBeanInfoBuilder topicInfoBuilder = new GBeanInfoBuilder(
062    //                AdminObjectWrapper.class, AdminObjectWrapperGBean.GBEAN_INFO);
063    //        topicInfoBuilder.addAttribute(new DynamicGAttributeInfo("PhysicalName",
064    //                String.class.getName(), true, true, true, true));
065    //        TOPIC_INFO = topicInfoBuilder.getBeanInfo();
066    //    }
067    
068        public void processAction(ActionRequest request, ActionResponse response)
069                throws IOException, PortletException {
070            String destinationName = request.getParameter(DESTINATION_NAME);
071            String destinationPhysicalName = request
072                    .getParameter(DESTINATION_PHYSICAL_NAME);
073            String destinationType = request.getParameter(DESTINATION_TYPE);
074            String destinationApplicationName = request
075                    .getParameter(DESTINATION_APPLICATION_NAME);
076            String destinationModuleName = request
077                    .getParameter(DESTINATION_MODULE_NAME);
078            try {
079    
080                Artifact configId = new Artifact(Artifact.DEFAULT_GROUP_ID, BASE_CONFIG_URI + destinationName, "0", "car");
081                ConfigurationData configurationData = new ConfigurationData(configId, kernel.getNaming());
082                configurationData.getEnvironment().addDependency(new Dependency(ACTIVEMQ_ARTIFACT, ImportType.ALL));
083    
084                AbstractName adminObjectName = kernel.getNaming().createRootName(configId, destinationName, NameFactory.JCA_ADMIN_OBJECT);
085    //            ObjectName adminObjectName = NameFactory.getComponentName(null,
086    //                    null, destinationApplicationName, NameFactory.JCA_RESOURCE,
087    //                    destinationModuleName, destinationName, null, baseContext);
088    
089                GBeanData adminObjectData;
090                if (Topic.class.getName().equals(destinationType)) {
091                    adminObjectData = getTopicGBeanData();
092                    // If we are adding a topic we have to add a browser so we can view
093                    // its messages later.
094                    AbstractName browserName = kernel.getNaming().createChildName(adminObjectName, destinationName, "TopicBrowser");
095                    GBeanData tBrowserBeanData = new GBeanData(browserName, TopicBrowserGBean.GBEAN_INFO);
096                    tBrowserBeanData.setAttribute("subscriberName", destinationName);
097                    tBrowserBeanData.setReferencePattern("ConnectionFactoryWrapper", JCA_MANAGED_CONNECTION_FACTORY_NAME);
098                    tBrowserBeanData.setReferencePattern("TopicWrapper",
099                            adminObjectName);
100    
101                    configurationData.addGBean(tBrowserBeanData);
102                } else if (Queue.class.getName().equals(destinationType)) {
103                    adminObjectData = getQueueGBeanData();
104                } else {
105                    throw new PortletException(
106                            "Invalid choice destination, must be FQCL of Topic or Queue, not "
107                                    + destinationType);
108                }
109                adminObjectData.setAbstractName(adminObjectName);
110                adminObjectData.setAttribute("PhysicalName",
111                        destinationPhysicalName);
112                configurationData.addGBean(adminObjectData);
113    
114    
115                ConfigurationManager configurationManager = ConfigurationUtil
116                        .getConfigurationManager(kernel);
117                List stores = configurationManager.listStores();
118                assert stores.size() == 1 : "Piling one hack on another, this code only works with exactly one store";
119    
120                ObjectName storeName = (ObjectName) stores.iterator().next();
121                File installDir = (File) kernel.invoke(storeName,
122                        "createNewConfigurationDir");
123    //            Environment environment = new Environment();
124    //            environment.setConfigId(configId);
125    //            environment.addDependency(parentId, ImportType.ALL);
126    //            List gbeans = new ArrayList();
127    //            gbeans.add(adminObjectData);
128                //TODO configid FIXME set configurationDir correctly
129                File configurationDir = null;
130    //            ConfigurationData configData = new ConfigurationData(ConfigurationModuleType.SERVICE,
131    //                    new LinkedHashSet(),
132    //                    gbeans,
133    //                    Collections.EMPTY_LIST,
134    //                    environment, configurationDir,
135    //                    kernel.getNaming());
136    
137                //saves it.
138                //deploymentContext.close();
139                kernel.invoke(storeName, "install", new Object[] {configurationData, installDir},
140                        new String[] {ConfigurationData.class.getName(), File.class.getName() });
141    
142                configurationManager.loadConfiguration(configId);
143                configurationManager.startConfiguration(configId);
144    
145            } catch (Exception e) {
146                log.error("problem", e);
147            }
148            response.setRenderParameter("processAction", "viewDestinations");
149        }
150    
151    }