Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Last active December 27, 2015 17:49
Show Gist options
  • Save cfalzone/7365013 to your computer and use it in GitHub Desktop.
Save cfalzone/7365013 to your computer and use it in GitHub Desktop.
DotCMS Extended Bundle Activator that can help deploy jsps and assets (as well as other files) into the dotCMS repo.
package com.aquent.osgi;
import org.osgi.framework.BundleContext;
public class AquentActivator extends ExtendedBundleActivator {
@Override
public void start ( BundleContext ctx ) throws Exception {
// Initializing services
initializeServices(ctx);
// Add the resources
deployJSPs(ctx);
deployAssets(ctx);
}
@Override
public void stop ( BundleContext ctx ) throws Exception {
// Remove the Resources
undeployJSPs(ctx);
undeployAssets(ctx);
}
}
package com.aquent.osgi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import org.osgi.framework.BundleContext;
import com.dotmarketing.osgi.GenericBundleActivator;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
public abstract class ExtendedBundleActivator extends GenericBundleActivator {
protected void deployJSPs(BundleContext context) throws Exception {
String destFolder = Config.CONTEXT.getRealPath(File.separator);
moveResources(context, "jsp", destFolder);
}
protected void deployAssets(BundleContext context) throws Exception {
String assetsDir;
if (UtilMethods.isSet(Config.getStringProperty("ASSET_REAL_PATH"))) {
assetsDir = Config.getStringProperty("ASSET_REAL_PATH");
} else {
assetsDir = Config.CONTEXT.getRealPath(File.separator + Config.getStringProperty("ASSET_PATH")) + File.separator;
}
moveResources(context, "assets", assetsDir);
}
protected void moveResources ( BundleContext context, String sourceFolder, String destFolder ) throws Exception {
//Find all the resources under that folder
Enumeration<URL> entries = context.getBundle().findEntries( sourceFolder, "*.*", true );
while ( entries.hasMoreElements() ) {
URL entryUrl = entries.nextElement();
String entryPath = entryUrl.getPath();
String fileName = entryPath.substring( entryPath.lastIndexOf('/')+1, entryPath.length() );
String resourceFilePath = destFolder + fileName;
File resourceFile = new File( resourceFilePath );
if ( !resourceFile.exists() ) {
Logger.info(this, "Deploying file: "+resourceFilePath);
InputStream in = null;
OutputStream out = null;
try {
if ( !resourceFile.getParentFile().exists() ) {
resourceFile.getParentFile().mkdirs();
}
resourceFile.createNewFile();
in = entryUrl.openStream();
out = new FileOutputStream( resourceFile );
byte[] buffer = new byte[1024];
int length;
while ( (length = in.read( buffer )) > 0 ) {
out.write( buffer, 0, length );
}
} finally {
if ( in != null ) {
in.close();
}
if ( out != null ) {
out.flush();
out.close();
}
}
} else {
Logger.warn(this, "File Already Exists, not deploying: "+resourceFilePath);
}
}
}
protected void undeployJSPs(BundleContext context) throws Exception {
String destFolder = Config.CONTEXT.getRealPath(File.separator);
removeResources(context, "jsp", destFolder);
}
protected void undeployAssets(BundleContext context) throws Exception {
String assetsDir;
if (UtilMethods.isSet(Config.getStringProperty("ASSET_REAL_PATH"))) {
assetsDir = Config.getStringProperty("ASSET_REAL_PATH");
} else {
assetsDir = Config.CONTEXT.getRealPath(File.separator + Config.getStringProperty("ASSET_PATH")) + File.separator;
}
removeResources(context, "assets", assetsDir);
}
protected void removeResources( BundleContext context, String sourceFolder, String destFolder ) throws Exception {
//Find all the resources under that folder
Enumeration<URL> entries = context.getBundle().findEntries( sourceFolder, "*.*", true );
while ( entries.hasMoreElements() ) {
URL entryUrl = entries.nextElement();
String entryPath = entryUrl.getPath();
String fileName = entryPath.substring( entryPath.lastIndexOf('/')+1, entryPath.length() );
String resourceFilePath = destFolder + fileName;
File resourceFile = new File( resourceFilePath );
if ( resourceFile.exists() ) {
Logger.info(this, "Removing file: "+resourceFilePath);
resourceFile.delete();
} else {
Logger.warn(this, "File Does not Exists, not removing: "+resourceFilePath);
}
}
}
}
@ethode
Copy link

ethode commented Apr 27, 2014

Chris, can you add the entire plugin package with the Manifest? I'm going to implement this, this weekend so I can override JSP's using an OSGi plugin, I think other would like this as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment