You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by sg...@apache.org on 2005/03/01 11:59:06 UTC
cvs commit: jakarta-turbine-fulcrum/resourcemanager/src/java/org/apache/fulcrum/resourcemanager/impl BaseResourceManager.java FileResourceManager.java
sgoeschl 2005/03/01 02:59:06
Modified: resourcemanager/src/java/org/apache/fulcrum/resourcemanager/impl
BaseResourceManager.java FileResourceManager.java
Log:
Synchronizing my development CVS with Fulcrum
Revision Changes Path
1.2 +281 -6 jakarta-turbine-fulcrum/resourcemanager/src/java/org/apache/fulcrum/resourcemanager/impl/BaseResourceManager.java
Index: BaseResourceManager.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/resourcemanager/src/java/org/apache/fulcrum/resourcemanager/impl/BaseResourceManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BaseResourceManager.java 31 Jan 2005 13:36:27 -0000 1.1
+++ BaseResourceManager.java 1 Mar 2005 10:59:06 -0000 1.2
@@ -19,10 +19,12 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.util.Properties;
import org.apache.avalon.framework.activity.Disposable;
@@ -30,6 +32,7 @@
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.Reconfigurable;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
@@ -38,6 +41,7 @@
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.fulcrum.resourcemanager.ResourceManager;
+import org.apache.fulcrum.pbe.PBEService;
/**
* Base class for a service implementation capturing the Avalon
@@ -48,7 +52,8 @@
public abstract class BaseResourceManager
extends AbstractLogEnabled
- implements Contextualizable, Serviceable, Configurable, Initializable, Disposable, ResourceManager
+ implements Contextualizable, Serviceable, Configurable,
+ Initializable, Disposable, Reconfigurable, ResourceManager
{
/** The context supplied by the avalon framework */
private Context context;
@@ -68,6 +73,16 @@
/** the name of the domain */
private String domain;
+ /** the seed to generate the password */
+ private String seed;
+
+ /** use transparent encryption/decryption */
+ private String useEncryption;
+
+ /////////////////////////////////////////////////////////////////////////
+ // Avalon Service Lifecycle Implementation
+ /////////////////////////////////////////////////////////////////////////
+
/**
* Constructor
*/
@@ -100,10 +115,8 @@
public void configure(Configuration configuration) throws ConfigurationException
{
this.configuration = configuration;
-
- // extract the domain name
-
this.setDomain( configuration.getAttribute("name") );
+ this.seed = "resourcemanager";
}
/**
@@ -120,12 +133,27 @@
public void dispose()
{
this.applicationDir = null;
- this.tempDir = null;
+ this.configuration = null;
this.context = null;
+ this.domain = null;
+ this.seed = null;
this.serviceManager = null;
- this.configuration = null;
+ this.tempDir = null;
+ }
+
+ /**
+ * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
+ */
+ public void reconfigure(Configuration configuration)
+ throws ConfigurationException
+ {
+ this.configure(configuration);
}
+ /////////////////////////////////////////////////////////////////////////
+ // Service Implementation
+ /////////////////////////////////////////////////////////////////////////
+
/**
* @return Returns the configuration.
*/
@@ -242,4 +270,251 @@
{
this.domain = domain;
}
+
+ /**
+ * @return Returns the useEncryption.
+ */
+ protected String getUseEncryption()
+ {
+ return useEncryption;
+ }
+
+ /**
+ * @param useEncryption The useEncryption to set.
+ */
+ protected void setUseEncryption(String useEncryption)
+ {
+ this.useEncryption = useEncryption;
+ }
+
+ /**
+ * @return the instance of the PBEService
+ */
+ protected PBEService getPBEService()
+ {
+ String service = PBEService.class.getName();
+ PBEService result = null;
+
+ if( this.getServiceManager().hasService(service) )
+ {
+ try
+ {
+ result = (PBEService) this.getServiceManager().lookup(service);
+ }
+ catch (ServiceException e)
+ {
+ String msg = "The PBEService can't be accessed";
+ this.getLogger().error( msg, e );
+ throw new RuntimeException( msg );
+ }
+ }
+ else
+ {
+ String msg = "The PBEService is not registered";
+ throw new RuntimeException( msg );
+ }
+
+ return result;
+ }
+
+ /**
+ * @return the password for the resource manager
+ */
+ private char[] getPassword() throws Exception
+ {
+ return this.getPBEService().createPassword( this.seed.toCharArray() );
+ }
+
+ /**
+ * Reads the given file and decrypts it if required
+ * @param source the source file
+ * @return the content of the file
+ */
+ protected byte[] read( InputStream is )
+ throws IOException
+ {
+ if( this.getUseEncryption().equalsIgnoreCase("true") )
+ {
+ return readEncrypted( is );
+ }
+ else if( this.getUseEncryption().equalsIgnoreCase("auto") )
+ {
+ return readSmartEncrypted( is );
+ }
+ else
+ {
+ return readPlain( is );
+ }
+ }
+
+ /**
+ * Reads from an unencrypted input stream
+ * @param is the source input stream
+ * @return the content of the input stream
+ */
+ private byte[] readPlain( InputStream is )
+ throws IOException
+ {
+ byte[] result = null;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ this.copy(is,baos);
+ result = baos.toByteArray();
+ return result;
+ }
+
+ /**
+ * Reads a potentially encrypted input stream.
+ * @param is the source input stream
+ * @return the content of the input stream
+ */
+ private byte[] readSmartEncrypted( InputStream is )
+ throws IOException
+ {
+ byte[] result = null;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ try
+ {
+ char[] password = getPassword();
+ InputStream sdis = this.getPBEService().getSmartInputStream( is, password );
+ this.copy( sdis, baos );
+ result = baos.toByteArray();
+ return result;
+ }
+ catch (IOException e)
+ {
+ String msg = "Failed to process the input stream";
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ catch (Exception e)
+ {
+ String msg = "Failed to decrypt the input stream";
+ this.getLogger().error( msg, e );
+ throw new IOException( msg );
+ }
+ }
+
+ /**
+ * Reads a potentially encrypted input stream.
+ * @param is the source input stream
+ * @return the content of the input stream
+ */
+ private byte[] readEncrypted( InputStream is )
+ throws IOException
+ {
+ byte[] result = null;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ try
+ {
+ char[] password = getPassword();
+ InputStream sdis = this.getPBEService().getInputStream( is, password );
+ this.copy( sdis, baos );
+ result = baos.toByteArray();
+ return result;
+ }
+ catch (IOException e)
+ {
+ String msg = "Failed to process the input stream";
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ catch (Exception e)
+ {
+ String msg = "Failed to decrypt the input stream";
+ this.getLogger().error( msg, e );
+ throw new IOException( msg );
+ }
+ }
+
+ /**
+ * Write the given file and encrypts it if required
+ * @param target the target file
+ * @parwm content the content to be written
+ * @return
+ */
+ protected void write( OutputStream os, byte[] content )
+ throws IOException
+ {
+ if( this.getUseEncryption().equalsIgnoreCase("true") )
+ {
+ writeEncrypted( os, content );
+ }
+ else if( this.getUseEncryption().equalsIgnoreCase("auto") )
+ {
+ writeEncrypted( os, content );
+ }
+ else
+ {
+ writePlain( os, content );
+ }
+ }
+
+ /**
+ * Write the given file without encryption.
+ * @param target the target file
+ * @parwm content the content to be written
+ * @return
+ */
+ private void writePlain( OutputStream os, byte[] content )
+ throws IOException
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(content);
+ this.copy( bais, os );
+ }
+
+ /**
+ * Write the given file and encrypt it.
+ * @param target the target file
+ * @parwm content the content to be written
+ * @return
+ */
+ private void writeEncrypted( OutputStream os, byte[] content )
+ throws IOException
+ {
+ try
+ {
+ char[] password = this.getPassword();
+ ByteArrayInputStream bais = new ByteArrayInputStream(content);
+ OutputStream eos = this.getPBEService().getOutputStream( os, password );
+ this.copy(bais,eos);
+ }
+ catch (IOException e)
+ {
+ String msg = "Failed to process the output stream";
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ catch (Exception e)
+ {
+ String msg = "Failed to encrypt the input stream";
+ this.getLogger().error( msg, e );
+ throw new IOException( msg );
+ }
+ }
+
+ /**
+ * Pumps the input stream to the output stream.
+ *
+ * @param is the source input stream
+ * @param os the target output stream
+ * @throws IOException the copying failed
+ */
+ private void copy( InputStream is, OutputStream os )
+ throws IOException
+ {
+ byte[] buf = new byte[1024];
+ int n = 0;
+ int total = 0;
+
+ while ((n = is.read(buf)) > 0)
+ {
+ os.write(buf, 0, n);
+ total += n;
+ }
+
+ os.flush();
+ os.close();
+ }
}
1.2 +26 -14 jakarta-turbine-fulcrum/resourcemanager/src/java/org/apache/fulcrum/resourcemanager/impl/FileResourceManager.java
Index: FileResourceManager.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/resourcemanager/src/java/org/apache/fulcrum/resourcemanager/impl/FileResourceManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileResourceManager.java 31 Jan 2005 13:36:29 -0000 1.1
+++ FileResourceManager.java 1 Mar 2005 10:59:06 -0000 1.2
@@ -51,7 +51,7 @@
/** the cached list of all available resources */
private String[] resourceFileNameList;
-
+
/////////////////////////////////////////////////////////////////////////
// Avalon Service Lifecycle Implementation
/////////////////////////////////////////////////////////////////////////
@@ -79,6 +79,12 @@
this.useLocator = cfg.getChild(CONFIG_KEY_USELOCATOR).getValueAsBoolean(false);
+ // are we using encryption/decryption
+
+ this.setUseEncryption(
+ cfg.getChild(CONFIG_KEY_USEENCRYPTION).getValue("false")
+ );
+
// locate the directory where we the resources are located
String currLocationName = cfg.getChild(CONFIG_KEY_LOCATION).getValue();
@@ -125,7 +131,17 @@
this.resourceDir = null;
this.resourceFileNameList = null;
}
-
+
+ /**
+ * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
+ */
+ public void reconfigure(Configuration configuration)
+ throws ConfigurationException
+ {
+ super.reconfigure( configuration );
+ this.configure(configuration);
+ }
+
/////////////////////////////////////////////////////////////////////////
// Service interface implementation
/////////////////////////////////////////////////////////////////////////
@@ -178,15 +194,12 @@
throws IOException
{
File resourceFile = new File( this.getResourceDir(), resourcePath );
-
this.getLogger().debug( "Creating resource : " + resourceFile.getAbsolutePath() );
-
- byte[] byteContent = this.getContent( resourceContent);
- FileOutputStream fos = new FileOutputStream( resourceFile );
- fos.write(byteContent);
+ byte[] byteContent = this.getContent( resourceContent);
+ FileOutputStream fos = new FileOutputStream(resourceFile);
+ this.write( fos, byteContent );
fos.flush();
fos.close();
-
this.createResourceFileNameList();
}
@@ -203,10 +216,9 @@
if( resourceFile != null )
{
this.getLogger().debug( "Loading the resource : " + resourceFile.getAbsolutePath() );
- FileInputStream fis = new FileInputStream(resourceFile);
- result = new byte[fis.available()];
- fis.read(result);
- fis.close();
+ FileInputStream fis = new FileInputStream(resourceFile);
+ result = this.read( fis );
+ fis.close();
return result;
}
else
@@ -346,7 +358,7 @@
{
return this.resourceDir;
}
-
+
/**
* Finds the resource file for the given name.
* @param resourceName the script name
@@ -497,5 +509,5 @@
result.append( resourceName );
return result.toString();
- }
+ }
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org