[db4o-om commit] r38 - in trunk/objectmanager-api/src/com/db4o/objectmanager: configuration model

2 views
Skip to first unread message

codesite...@google.com

unread,
Apr 15, 2008, 1:02:24 AM4/15/08
to db4o-om-c...@googlegroups.com
Author: gishac
Date: Mon Apr 14 21:57:43 2008
New Revision: 38

Added:
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/ConfigurationFacade.java
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/ConfigurationProvider.java
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/EncryptionProviderHandler.java
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/JarLoader.java
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/JarResources.java
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/MultiClassLoader.java
Modified:
trunk/objectmanager-api/src/com/db4o/objectmanager/model/Db4oConnectionSpec.java

Log:
Primary support for encryption

Added: trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/ConfigurationFacade.java
==============================================================================
--- (empty file)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/ConfigurationFacade.java
Mon Apr 14 21:57:43 2008
@@ -0,0 +1,43 @@
+/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */
+/**
+ * User: gishac
+ * Date: Apr 14, 2008
+ * Time: 11:42:03 PM
+ */
+package com.db4o.objectmanager.configuration;
+
+import com.db4o.io.IoAdapter;
+
+public class ConfigurationFacade {
+
+ transient IoAdapter _encryptionProvider;
+ private boolean _unicode;
+ private boolean _lockDatabaseFile;
+ private boolean _callConstructors;
+
+
+ public IoAdapter encryptionProvider() {
+ return _encryptionProvider;
+ }
+ public void encryptionProvider(IoAdapter provider) {
+ _encryptionProvider = provider;
+ }
+ public boolean unicode() {
+ return _unicode;
+ }
+ public void unicode(boolean enabled) {
+ _unicode = enabled;
+ }
+ public boolean lockDatabaseFile() {
+ return _lockDatabaseFile;
+ }
+ public void lockDatabaseFile(boolean lockDatabaseFile) {
+ _lockDatabaseFile = lockDatabaseFile;
+ }
+ public boolean callConstructors() {
+ return _callConstructors;
+ }
+ public void callConstructors(boolean callConstructors) {
+ _callConstructors = callConstructors;
+ }
+}

Added: trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/ConfigurationProvider.java
==============================================================================
--- (empty file)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/ConfigurationProvider.java
Mon Apr 14 21:57:43 2008
@@ -0,0 +1,128 @@
+/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */
+/**
+ * User: gishac
+ * Date: Feb 02, 2008
+ * Time: 10:15:25 PM
+ */
+
+
+package com.db4o.objectmanager.configuration;
+
+import java.io.Serializable;
+
+import com.db4o.Db4o;
+import com.db4o.config.Configuration;
+import com.db4o.io.IoAdapter;
+
+/**
+ *
+ * @author gish@c
+ *
+ */
+public class ConfigurationProvider implements Serializable {
+
+ transient private ConfigurationFacade _configuration;
+ transient IoAdapter _encryptionProvider;
+ private boolean _unicodeEnabled;
+ private boolean _lockDatabase;
+ private boolean _callConstructors;
+
+ public ConfigurationProvider() {
+
+ }
+
+ public void encryptionProvider(IoAdapter encryptionProvider) {
+ this._encryptionProvider = encryptionProvider;
+ }
+
+ public IoAdapter encryptionProvider()
+ {
+ return this._encryptionProvider;
+ }
+
+ public void unicodeEnabled(boolean unicodeEnabled){
+ this._unicodeEnabled = unicodeEnabled;
+ }
+
+ public boolean unicodeEnabled(){
+ return _unicodeEnabled;
+ }
+
+ public void lockDatabase(boolean lockDatabase){
+ this._lockDatabase = lockDatabase;
+ }
+
+ public boolean lockDatabase(){
+ return this._lockDatabase;
+ }
+
+ public ConfigurationFacade configuration() {
+ return _configuration;
+ }
+
+ public void configuration(ConfigurationFacade configuration) {
+ this._configuration = configuration;
+ }
+
+ public void callConstructors(boolean callConstructors){
+ this._callConstructors = callConstructors;
+ }
+
+ public boolean callConstructors(){
+ return this._callConstructors;
+ }
+
+ /**
+ * Build the configuration object
+ * @param encryptionProvider
+ * @param unicodeEnabled
+ * @param lockDatabase
+ * @param callConstructors
+ * @return
+ */
+ public ConfigurationFacade buildConfiguration(IoAdapter
encryptionProvider, boolean unicodeEnabled,
+ boolean lockDatabase, boolean callConstructors)
+ {
+ this._unicodeEnabled = unicodeEnabled;
+ this._lockDatabase = lockDatabase;
+ this._callConstructors = callConstructors;
+ this._encryptionProvider = encryptionProvider;
+ return buildConfiguration();
+ }
+
+ /**
+ * Build the configuration object
+ * @return
+ */
+ public ConfigurationFacade buildConfiguration(){
+ _configuration = new ConfigurationFacade();
+ _configuration.callConstructors(callConstructors());
+ _configuration.unicode(unicodeEnabled());
+ _configuration.lockDatabaseFile(lockDatabase());
+ if(encryptionProvider() != null)
+ _configuration.encryptionProvider(encryptionProvider());
+ return _configuration;
+ }
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

Added: trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/EncryptionProviderHandler.java
==============================================================================
--- (empty file)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/EncryptionProviderHandler.java
Mon Apr 14 21:57:43 2008
@@ -0,0 +1,41 @@
+package com.db4o.objectmanager.configuration;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import com.db4o.io.IoAdapter;
+
+public class EncryptionProviderHandler {
+
+ public static IoAdapter getEncryptionProvider(Class clazz, String password)
+ throws SecurityException, NoSuchMethodException,
+ IllegalArgumentException, InstantiationException,
+ IllegalAccessException, InvocationTargetException {
+ Object instance = null;
+ if (clazz != null) {
+ Constructor[] constructors = clazz.getConstructors();
+ if (constructors.length > 0) {
+ Constructor ctor;
+ //Try to find a default constructor which receives the password
+ ctor = clazz.getConstructor(String.class);
+ if (ctor == null) {
+ //Use the default constructor
+ //We have to set the password but the IoAdapter class doesn't
defines an standard
+ //interface in order to set the password property via reflections
+ ctor = clazz.getConstructor();
+ instance = ctor.newInstance();
+ } else {
+ instance = ctor.newInstance(password);
+ }
+ if (instance.getClass().getSuperclass() == IoAdapter.class)
+ System.out.println("Created IoAdapter: " + clazz.getName());
+ else
+ System.out.println("Created but not IoAdapter: "
+ + clazz.getName());
+ }
+ }
+ return (instance != null)? (IoAdapter) instance : null;
+
+ }
+
+}

Added: trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/JarLoader.java
==============================================================================
--- (empty file)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/JarLoader.java
Mon Apr 14 21:57:43 2008
@@ -0,0 +1,56 @@
+package com.db4o.objectmanager.configuration;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import com.db4o.io.IoAdapter;
+
+public class JarLoader extends MultiClassLoader {
+
+ private JarResources jarResource;
+
+ public JarLoader(String jarPath) {
+ jarResource = new JarResources(jarPath);
+ }
+
+ @Override
+ protected byte[] loadClassBytes(String className) {
+ className = formatClassName(className);
+ return jarResource.getResource(className);
+ }
+
+ public List<Class> getClasses(String path) {
+ List<Class> classes = new ArrayList<Class>();
+ try {
+ JarLoader jarLoader = new JarLoader(path);
+ JarFile jarFile = new JarFile(path);
+ for (Enumeration<JarEntry> e = jarFile.entries(); e
+ .hasMoreElements();) {
+ try {
+ JarEntry entry = e.nextElement();
+ if (entry.getName().endsWith(".class")) {
+ String className = entry.getName().substring(0,
+ entry.getName().indexOf(".class")).replace("/",
+ ".").replace("\\", ".");
+
+ Class clazz = jarLoader.loadClass(className);
+ clazz.asSubclass(IoAdapter.class);
+ if (clazz != null) {
+ classes.add(clazz);
+ }
+ }
+ } catch (Exception ex) {
+ }
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return classes;
+ }
+
+}

Added: trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/JarResources.java
==============================================================================
--- (empty file)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/JarResources.java
Mon Apr 14 21:57:43 2008
@@ -0,0 +1,176 @@
+package com.db4o.objectmanager.configuration;
+
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+
+/**
+ * JarResources: JarResources maps all resources included in a Zip or
Jar file.
+ * Additionaly, it provides a method to extract one as a blob.
+ */
+public final class JarResources {
+
+ // external debug flag
+ public boolean debugOn = false;
+
+ // jar resource mapping tables
+ private Hashtable htSizes = new Hashtable();
+ private Hashtable htJarContents = new Hashtable();
+
+ // a jar file
+ private String jarFileName;
+
+ /**
+ * creates a JarResources. It extracts all resources from a Jar into an
+ * internal hashtable, keyed by resource names.
+ *
+ * @param jarFileName
+ * a jar or zip file
+ */
+ public JarResources(String jarFileName) {
+ this.jarFileName = jarFileName;
+ init();
+ }
+
+ /**
+ * Extracts a jar resource as a blob.
+ *
+ * @param name
+ * a resource name.
+ */
+ public byte[] getResource(String name) {
+ return (byte[]) htJarContents.get(name);
+ }
+
+ /** initializes internal hash tables with Jar file resources. */
+ private void init() {
+ try {
+ // extracts just sizes only.
+ ZipFile zf = new ZipFile(jarFileName);
+ Enumeration e = zf.entries();
+ while (e.hasMoreElements()) {
+ ZipEntry ze = (ZipEntry) e.nextElement();
+
+ if (debugOn) {
+ System.out.println(dumpZipEntry(ze));
+ }
+
+ htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
+ }
+ zf.close();
+
+ // extract resources and put them into the hashtable.
+ FileInputStream fis = new FileInputStream(jarFileName);
+ BufferedInputStream bis = new BufferedInputStream(fis);
+ ZipInputStream zis = new ZipInputStream(bis);
+ ZipEntry ze = null;
+ while ((ze = zis.getNextEntry()) != null) {
+ if (ze.isDirectory()) {
+ continue;
+ }
+
+ if (debugOn) {
+ System.out.println("ze.getName()=" + ze.getName() + ","
+ + "getSize()=" + ze.getSize());
+ }
+
+ int size = (int) ze.getSize();
+ // -1 means unknown size.
+ if (size == -1) {
+ size = ((Integer) htSizes.get(ze.getName())).intValue();
+ }
+
+ byte[] b = new byte[(int) size];
+ int rb = 0;
+ int chunk = 0;
+ while (((int) size - rb) > 0) {
+ chunk = zis.read(b, rb, (int) size - rb);
+ if (chunk == -1) {
+ break;
+ }
+ rb += chunk;
+ }
+
+ // add to internal resource hashtable
+ htJarContents.put(ze.getName(), b);
+
+ if (debugOn) {
+ System.out.println(ze.getName() + " rb=" + rb + ",size="
+ + size + ",csize=" + ze.getCompressedSize());
+ }
+ }
+ } catch (NullPointerException e) {
+ System.out.println("done.");
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Dumps a zip entry into a string.
+ *
+ * @param ze
+ * a ZipEntry
+ */
+ private String dumpZipEntry(ZipEntry ze) {
+ StringBuffer sb = new StringBuffer();
+ if (ze.isDirectory()) {
+ sb.append("d ");
+ } else {
+ sb.append("f ");
+ }
+
+ if (ze.getMethod() == ZipEntry.STORED) {
+ sb.append("stored ");
+ } else {
+ sb.append("defalted ");
+ }
+
+ sb.append(ze.getName());
+ sb.append("\t");
+ sb.append("" + ze.getSize());
+ if (ze.getMethod() == ZipEntry.DEFLATED) {
+ sb.append("/" + ze.getCompressedSize());
+ }
+
+ return (sb.toString());
+ }
+
+ /**
+ * Is a test driver. Given a jar file and a resource name, it trys to
+ * extract the resource and then tells us whether it could or not.
+ *
+ * <strong>Example</strong> Let's say you have a JAR file which
jarred up a
+ * bunch of gif image files. Now, by using JarResources, you could extract,
+ * create, and display those images on-the-fly.
+ *
+ * <pre>
+ * ...
+ * JarResources JR=new JarResources(&quot;GifBundle.jar&quot;);
+ * Image image=Toolkit.createImage(JR.getResource(&quot;logo.gif&quot;);
+ * Image logo=Toolkit.getDefaultToolkit().createImage(
+ * JR.getResources(&quot;logo.gif&quot;)
+ * );
+ * ...
+ * </pre>
+ */
+ public static void main(String[] args) throws IOException {
+ if (args.length != 2) {
+ System.err
+ .println("usage: java JarResources <jar file name> <resource name>");
+ System.exit(1);
+ }
+
+ JarResources jr = new JarResources(args[0]);
+ byte[] buff = jr.getResource(args[1]);
+ if (buff == null) {
+ System.out.println("Could not find " + args[1] + ".");
+ } else {
+ System.out.println("Found " + args[1] + " (length=" + buff.length
+ + ").");
+ }
+ }
+
+} // End of JarResources class.

Added: trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/MultiClassLoader.java
==============================================================================
--- (empty file)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/configuration/MultiClassLoader.java
Mon Apr 14 21:57:43 2008
@@ -0,0 +1,129 @@
+package com.db4o.objectmanager.configuration;
+
+import java.util.Hashtable;
+
+/**
+ * A simple test class loader capable of loading from multiple
sources, such as
+ * local files or a URL.
+ *
+ * This class is derived from an article by Chuck McManis
+ * http://www.javaworld.com/javaworld/jw-10-1996/indepth.src.html with large
+ * modifications.
+ *
+ * Note that this has been updated to use the non-deprecated version of
+ * defineClass() -- JDM.
+ *
+ * @author Jack Harich - 8/18/97
+ * @author John D. Mitchell - 99.03.04
+ */
+public abstract class MultiClassLoader extends ClassLoader {
+
+ // ---------- Fields --------------------------------------
+ private Hashtable<String,Class> classes = new Hashtable<String,Class>();
+ private char classNameReplacementChar;
+
+ protected boolean monitorOn = false;
+ protected boolean sourceMonitorOn = true;
+
+ // ---------- Initialization ------------------------------
+ public MultiClassLoader() {
+ }
+
+ // ---------- Superclass Overrides ------------------------
+ /**
+ * This is a simple version for external clients since they will
always want
+ * the class resolved before it is returned to them.
+ */
+ public Class loadClass(String className) throws
ClassNotFoundException {
+ return (loadClass(className, true));
+ }
+
+ // ---------- Abstract Implementation ---------------------
+ public synchronized Class loadClass(String className, boolean resolveIt)
+ throws ClassNotFoundException {
+
+ Class result;
+ byte[] classBytes;
+ monitor(">> MultiClassLoader.loadClass(" + className + ", " + resolveIt
+ + ")");
+
+ // ----- Check our local cache of classes
+ result = (Class) classes.get(className);
+ if (result != null) {
+ monitor(">> returning cached result.");
+ return result;
+ }
+
+ // ----- Check with the primordial class loader
+ try {
+ result = super.findSystemClass(className);
+ monitor(">> returning system class (in CLASSPATH).");
+ return result;
+ } catch (ClassNotFoundException e) {
+ monitor(">> Not a system class.");
+ }
+
+ // ----- Try to load it from preferred source
+ // Note loadClassBytes() is an abstract method
+ classBytes = loadClassBytes(className);
+ if (classBytes == null) {
+ throw new ClassNotFoundException();
+ }
+
+ // ----- Define it (parse the class file)
+ try {
+ result = defineClass(className, classBytes, 0, classBytes.length);
+ if (result == null) {
+ throw new ClassFormatError();
+ }
+ } catch (NoClassDefFoundError ex) {
+ //Dependencies were not found
+ }
+
+ // ----- Resolve if necessary
+ if (resolveIt)
+ resolveClass(result);
+
+ // Done
+ if (result == null)
+ return null;
+ classes.put(className, result);
+ monitor(">> Returning newly loaded class.");
+ return result;
+ }
+
+ // ---------- Public Methods ------------------------------
+ /**
+ * This optional call allows a class name such as "COM.test.Hello" to be
+ * changed to "COM_test_Hello", which is useful for storing classes from
+ * different packages in the same retrival directory. In the above example
+ * the char would be '_'.
+ */
+ public void setClassNameReplacementChar(char replacement) {
+ classNameReplacementChar = replacement;
+ }
+
+ // ---------- Protected Methods ---------------------------
+ protected abstract byte[] loadClassBytes(String className);
+
+ protected String formatClassName(String className) {
+ if (classNameReplacementChar == '\u0000') {
+ // '/' is used to map the package to the path
+ return className.replace('.', '/') + ".class";
+ } else {
+ // Replace '.' with custom char, such as '_'
+ return className.replace('.', classNameReplacementChar) + ".class";
+ }
+ }
+
+ protected void monitor(String text) {
+ if (monitorOn)
+ print(text);
+ }
+
+ // --- Std
+ protected static void print(String text) {
+ System.out.println(text);
+ }
+
+} // End class

Modified: trunk/objectmanager-api/src/com/db4o/objectmanager/model/Db4oConnectionSpec.java
==============================================================================
---
trunk/objectmanager-api/src/com/db4o/objectmanager/model/Db4oConnectionSpec.java (original)
+++
trunk/objectmanager-api/src/com/db4o/objectmanager/model/Db4oConnectionSpec.java
Mon Apr 14 21:57:43 2008
@@ -7,6 +7,7 @@
import com.db4o.config.*;
import com.db4o.instrumentation.classfilter.*;
import com.db4o.instrumentation.main.*;
+import com.db4o.objectmanager.configuration.ConfigurationFacade;
import com.db4o.reflect.jdk.*;
import com.db4o.ta.*;
import com.db4o.ta.instrumentation.*;
@@ -18,6 +19,7 @@
// Global temporary placeholder for read only setting.
// TODO: Move to preferences when starting to work on editing.
private static boolean PREFERENCE_IS_READ_ONLY = false;
+ private ConfigurationFacade configuration;

public static boolean preferenceIsReadOnly() {
return PREFERENCE_IS_READ_ONLY;
@@ -100,7 +102,21 @@
configureTA(config);
config.updateDepth(10);
config.add(new DotnetSupport());
+ applyConfiguratio(config);
return config;
+ }
+
+ private void applyConfiguratio(Configuration config){
+ if(configuration != null){
+ config.unicode(configuration.unicode());
+ config.io(configuration.encryptionProvider());
+ config.lockDatabaseFile(configuration.lockDatabaseFile());
+ config.callConstructors(configuration.callConstructors());
+ }
+ }
+
+ public void setConfiguration(ConfigurationFacade configuration){
+ this.configuration = configuration;
}

private void configureTA(Configuration config) {

Reply all
Reply to author
Forward
0 new messages