001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    
020    package org.apache.geronimo.mavenplugins.car;
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.util.List;
025    
026    import org.apache.geronimo.gbean.GBeanInfo;
027    import org.apache.geronimo.gbean.GBeanInfoBuilder;
028    import org.apache.geronimo.kernel.config.ConfigurationData;
029    import org.apache.geronimo.kernel.config.InvalidConfigException;
030    import org.apache.geronimo.kernel.config.NoSuchConfigException;
031    import org.apache.geronimo.kernel.repository.Artifact;
032    import org.apache.geronimo.kernel.repository.WritableListableRepository;
033    import org.apache.geronimo.kernel.Kernel;
034    import org.apache.geronimo.system.configuration.ExecutableConfigurationUtil;
035    import org.apache.geronimo.system.configuration.RepositoryConfigurationStore;
036    
037    /**
038     * Implementation of ConfigurationStore that loads Configurations from a repository.
039     * This implementation is read-only on the assumption that a separate maven task will
040     * handle installation of a built package into the repository.
041     *
042     * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
043     */
044    public class MavenConfigStore
045        extends RepositoryConfigurationStore
046    {
047        public MavenConfigStore(Kernel kernel, String objectName, WritableListableRepository repository) {
048            super(kernel, objectName, repository);
049        }
050    
051        public MavenConfigStore(WritableListableRepository repository) {
052            super(repository);
053        }
054    
055        public File createNewConfigurationDir(Artifact configId) {
056            try {
057                File tmpFile = File.createTempFile("package", ".tmpdir");
058                tmpFile.delete();
059                tmpFile.mkdir();
060                if (!tmpFile.isDirectory()) {
061                    return null;
062                }
063                // create the meta-inf dir
064                File metaInf = new File(tmpFile, "META-INF");
065                metaInf.mkdirs();
066                return tmpFile;
067            }
068            catch (IOException e) {
069                // doh why can't I throw this?
070                return null;
071            }
072        }
073    
074        public void install(ConfigurationData configurationData) throws IOException, InvalidConfigException {
075            File source = configurationData.getConfigurationDir();
076            if (!source.isDirectory()) {
077                throw new InvalidConfigException("Source must be a directory: " + source);
078            }
079            
080            Artifact configId = configurationData.getId();
081            File targetFile = repository.getLocation(configId);
082            ExecutableConfigurationUtil.createExecutableConfiguration(configurationData, null, targetFile);
083        }
084    
085        public void uninstall(Artifact configID) throws NoSuchConfigException, IOException {
086            File targetFile = repository.getLocation(configID);
087            targetFile.delete();
088        }
089    
090        public List listConfigurations() {
091            throw new UnsupportedOperationException();
092        }
093    
094        public static final GBeanInfo GBEAN_INFO;
095    
096        public static GBeanInfo getGBeanInfo() {
097            return GBEAN_INFO;
098        }
099    
100        static {
101            GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(MavenConfigStore.class, "ConfigurationStore");
102            builder.addAttribute("kernel", Kernel.class, false);
103            builder.addAttribute("objectName", String.class, false);
104            builder.addReference("Repository", WritableListableRepository.class, "Repository");
105            builder.setConstructor(new String[]{"kernel", "objectName", "Repository"});
106            GBEAN_INFO = builder.getBeanInfo();
107        }
108    }