Created
November 14, 2017 20:41
-
-
Save nkeiter/837fc63acb56f678661995eba1bb9b10 to your computer and use it in GitHub Desktop.
This file contains 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 edu.gettysburg.nkeiter.transportation.portlet.osgi; | |
import com.dotcms.repackage.org.apache.commons.io.FileUtils; | |
import com.dotcms.repackage.org.apache.logging.log4j.LogManager; | |
import com.dotcms.repackage.org.apache.logging.log4j.core.LoggerContext; | |
import com.dotmarketing.loggers.Log4jUtil; | |
import com.dotmarketing.osgi.GenericBundleActivator; | |
import com.dotmarketing.util.Config; | |
import com.dotmarketing.util.Logger; | |
import com.liferay.util.FileUtil; | |
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 javax.servlet.ServletContext; | |
import org.osgi.framework.BundleContext; | |
public class Activator extends GenericBundleActivator | |
{ | |
public static Class<Activator> clazz = Activator.class; | |
private LoggerContext pluginLoggerContext; | |
private static final String PLUGIN_NAME = "Transportation Portlets"; | |
private static final String PORTLET_1 = "REST_PORTLET_TRANSPORTATION_SCHEDULING"; | |
private static final String PORTLET_2 = "REST_PORTLET_TRANSPORTATION_CALENDAR"; | |
private static final String PORTLET_3 = "REST_PORTLET_TRANSPORTATION"; | |
private static final String[] PORTLETS = { PORTLET_1, PORTLET_2, PORTLET_3 }; | |
private void copyJSPs( BundleContext bundleContext ) | |
{ | |
for ( String portlet : PORTLETS ) | |
{ | |
String portletDirectory = portlet.toLowerCase(); | |
String containerFolder = "/jsp/" + portletDirectory + "/"; | |
ServletContext servletContext = Config.CONTEXT; | |
String destinationPath = servletContext.getRealPath( "WEB-INF" ); | |
try | |
{ | |
// Find all the JSP resources under this folder | |
Enumeration<URL> entries = bundleContext.getBundle().findEntries( containerFolder, "*.*", true ); | |
while ( entries.hasMoreElements() ) | |
{ | |
URL entryUrl = entries.nextElement(); | |
String entryPath = entryUrl.getPath(); | |
String resourceFilePath = destinationPath + entryPath; | |
File resourceFile = new File( resourceFilePath ); | |
if ( !resourceFile.exists() ) | |
{ | |
InputStream inputStream = null; | |
OutputStream outputStream = null; | |
try | |
{ | |
if ( !resourceFile.getParentFile().exists() ) | |
{ | |
resourceFile.getParentFile().mkdirs(); | |
} | |
resourceFile.createNewFile(); | |
inputStream = entryUrl.openStream(); | |
outputStream = new FileOutputStream( resourceFile ); | |
byte[] buffer = new byte[1024]; | |
int length; | |
while ( (length = inputStream.read( buffer )) > 0 ) | |
{ | |
outputStream.write( buffer, 0, length ); | |
} | |
} | |
catch ( Exception exception ) | |
{ | |
Logger.error( this, "Can't copy file [" + resourceFilePath + "]", exception ); | |
} | |
finally | |
{ | |
if ( inputStream != null ) | |
{ | |
inputStream.close(); | |
} | |
if ( outputStream != null ) | |
{ | |
outputStream.flush(); | |
outputStream.close(); | |
} | |
} | |
} | |
} | |
} | |
catch ( Exception exception ) | |
{ | |
Logger.error( this, "Can't copy files [" + portletDirectory + "]", exception ); | |
} | |
} | |
} | |
private void deleteJSPs() | |
{ | |
for ( String portlet : PORTLETS ) | |
{ | |
String portletDirectory = portlet.toLowerCase(); | |
ServletContext servletContext = Config.CONTEXT; | |
String destinationPath = servletContext.getRealPath( "WEB-INF" ) + "jsp/" + portletDirectory + "/"; | |
File destinationDirectory = new File ( destinationPath ); | |
try | |
{ | |
FileUtil.deltree( destinationDirectory ); | |
} | |
catch ( Exception exception ) | |
{ | |
Logger.error( this, "Can't delete directory [" + portletDirectory + "]", exception ); | |
} | |
} | |
} | |
@Override | |
public void start( BundleContext bundleContext ) throws Exception | |
{ | |
// Initializing log4j... | |
LoggerContext dotcmsLoggerContext = Log4jUtil.getLoggerContext(); | |
// Initialing the log4j context of this plugin based on the dotCMS logger context | |
this.pluginLoggerContext = (LoggerContext) LogManager.getContext( clazz.getClassLoader(), false, dotcmsLoggerContext, dotcmsLoggerContext.getConfigLocation() ); | |
Logger.info( this, "Got to start( BundleContext ) " + PLUGIN_NAME ); | |
// Initializing services... | |
initializeServices( bundleContext ); | |
// Copy JSPs | |
copyJSPs( bundleContext ); | |
// Register our portlets | |
String[] xmls = new String[]{ "conf/liferay-portlet.xml" }; | |
registerPortlets( bundleContext, xmls ); | |
} | |
@Override | |
public void stop( BundleContext bundleContext ) throws Exception | |
{ | |
Logger.info( this, "Got to stop( BundleContext ) " + PLUGIN_NAME ); | |
// Unregister all bundle services | |
unregisterServices( bundleContext ); | |
// Delete JSPs | |
deleteJSPs(); | |
// Shutting down log4j in order to avoid memory leaks | |
Log4jUtil.shutdown( this.pluginLoggerContext ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment