Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #127 from andreisavu/issue-127
Browse files Browse the repository at this point in the history
Refactor templates (remove the need for Java code)
  • Loading branch information
Andrei Savu committed Feb 8, 2013
2 parents 9ae5f0f + 1c5cb3b commit a07bf09
Show file tree
Hide file tree
Showing 50 changed files with 1,389 additions and 1,226 deletions.
42 changes: 41 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@
<artifactId>provisionr-core</artifactId>
<packaging>bundle</packaging>

<properties>
<osgi.import>*</osgi.import>
<osgi.export>com.axemblr.provisionr.core*</osgi.export>
</properties>

<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -55,6 +64,10 @@
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.fileinstall</artifactId>
</dependency>
<dependency>
<groupId>com.axemblr.provisionr</groupId>
<artifactId>provisionr-test-support</artifactId>
Expand Down Expand Up @@ -107,6 +120,11 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -125,7 +143,8 @@
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Export-Packages>com.axemblr.provisionr.core, *</Export-Packages>
<Import-Packages>${osgi.import}</Import-Packages>
<Export-Packages>${osgi.export}</Export-Packages>
</instructions>
</configuration>
</plugin>
Expand All @@ -151,6 +170,27 @@
<type>cfg</type>
<classifier>defaults</classifier>
</artifact>
<artifact>
<file>
${project.build.directory}/classes/org.apache.felix.fileinstall-templates.cfg
</file>
<type>cfg</type>
<classifier>fileinstall</classifier>
</artifact>
<artifact>
<file>
${project.build.directory}/classes/com/axemblr/provisionr/core/templates/cdh3.xml
</file>
<type>template</type>
<classifier>cdh3</classifier>
</artifact>
<artifact>
<file>
${project.build.directory}/classes/com/axemblr/provisionr/core/templates/jenkins.xml
</file>
<type>template</type>
<classifier>jenkins</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L
*
* Licensed 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 com.axemblr.provisionr.core.templates;

import com.axemblr.provisionr.core.templates.xml.XmlTemplate;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Maps;
import java.io.File;
import java.util.concurrent.ConcurrentMap;
import org.apache.felix.fileinstall.ArtifactInstaller;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Listener for pool template files
* <p/>
* When a new xml file is places under templates this class is
* notified and a new pool template is registered as a service
*/
public class PoolTemplateInstaller implements ArtifactInstaller {

private static final Logger LOG = LoggerFactory.getLogger(PoolTemplateInstaller.class);

public static final String TEMPLATES_FOLDER = "templates";
public static final String TEMPLATES_EXTENSION = ".xml";

/**
* A bundle context provided by the OSGi framework
*/
private final BundleContext bundleContext;

/**
* A map with all the pool templates registered by this listener
*/
private final ConcurrentMap<String, ServiceRegistration> templates;

public PoolTemplateInstaller(BundleContext bundleContext) {
this.bundleContext = checkNotNull(bundleContext, "bundleContext is null");
this.templates = Maps.newConcurrentMap();
}

@Override
public boolean canHandle(File file) {
return TEMPLATES_FOLDER.equals(file.getParentFile().getName())
&& file.getName().endsWith(TEMPLATES_EXTENSION);
}

/**
* Install a new pool template as a service using the file content
* <p/>
* The absolute file path is the unique identifier
*/
@Override
public void install(File file) throws Exception {
final String absolutePath = file.getAbsolutePath();
LOG.info("Installing Pool template from " + absolutePath);

if (!templates.containsKey(absolutePath)) {
PoolTemplate template = XmlTemplate.newXmlTemplate(file);
ServiceRegistration registration = bundleContext
.registerService(PoolTemplate.class.getName(), template, null);

templates.put(absolutePath, registration);
LOG.info("Registered new template with ID: " + template.getId());
}
}

/**
* Uninstall a pool description identified by the absolute file path
*/
@Override
public void uninstall(File file) throws Exception {
final String absolutePath = file.getAbsolutePath();
LOG.info("Uninstalling Pool template for path " + absolutePath);

if (templates.containsKey(absolutePath)) {
templates.remove(absolutePath).unregister();
}
}

/**
* Update a pool template
* <p/>
* This method performs no actions if there is no pool registered for this file
*/
@Override
public void update(File file) throws Exception {
if (templates.containsKey(file.getAbsolutePath())) {
uninstall(file);
install(file);
}
}
}
Loading

0 comments on commit a07bf09

Please sign in to comment.