Created
June 11, 2014 14:14
-
-
Save cfalzone/a6a6a7c4fa740d171bcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment