From: <mwl...@us...> - 2008-03-21 16:20:32
|
Revision: 668 http://cishell.svn.sourceforge.net/cishell/?rev=668&view=rev Author: mwlinnem Date: 2008-03-21 09:20:18 -0700 (Fri, 21 Mar 2008) Log Message: ----------- Initial import. Moved from NWB repository to the native homeland of the user preferences service. Added Paths: ----------- branches/user_prefs/org.cishell.reference.prefs.admin/.classpath branches/user_prefs/org.cishell.reference.prefs.admin/.project branches/user_prefs/org.cishell.reference.prefs.admin/.settings/ branches/user_prefs/org.cishell.reference.prefs.admin/META-INF/ branches/user_prefs/org.cishell.reference.prefs.admin/META-INF/MANIFEST.MF branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/algorithm.properties branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/component.xml branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/l10n/ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/l10n/bundle_en.properties branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/metatype/ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/metatype/METADATA.XML branches/user_prefs/org.cishell.reference.prefs.admin/build.properties branches/user_prefs/org.cishell.reference.prefs.admin/src/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefAdmin.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPage.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPageComparator.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceAD.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceOCD.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefsByService.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefAdminImpl.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefInfoGrabber.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefPageImpl.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefReferenceProcessor.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceADImpl.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceOCDImpl.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/TypePrefixes.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/util/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/util/BasicTree.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/util/ListMap.java branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/service/ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/service/prefadmin/ Added: branches/user_prefs/org.cishell.reference.prefs.admin/.classpath =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/.classpath (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/.classpath 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="output" path="build"/> +</classpath> Added: branches/user_prefs/org.cishell.reference.prefs.admin/.project =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/.project (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/.project 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.cishell.reference.prefs.admin</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> Added: branches/user_prefs/org.cishell.reference.prefs.admin/META-INF/MANIFEST.MF =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/META-INF/MANIFEST.MF (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/META-INF/MANIFEST.MF 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,20 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: CIShell Pref Admin +Bundle-SymbolicName: org.cishell.reference.prefs.admin +Bundle-Version: 0.0.1 +Bundle-ClassPath: . +Import-Package: org.cishell.framework, + org.cishell.framework.algorithm, + org.cishell.framework.data, + org.cishell.framework.preference, + org.cishell.reference.service.metatype, + org.osgi.framework;version="1.3.0", + org.osgi.service.cm;version="1.2.0", + org.osgi.service.component;version="1.0.0", + org.osgi.service.log;version="1.3.0", + org.osgi.service.metatype;version="1.1.0", + org.osgi.service.prefs;version="1.1.0" +X-AutoStart: true +Service-Component: OSGI-INF/component.xml +Export-Package: org.cishell.reference.prefs.admin Added: branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/algorithm.properties =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/algorithm.properties (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/algorithm.properties 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,2 @@ +service.pid=org.cishell.reference.prefs.admin.PrefAdmin +remoteable=false Added: branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/component.xml =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/component.xml (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/component.xml 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<component name="org.cishell.reference.prefs.admin.internal.PrefAdmin.component" immediate="true"> + <implementation class="org.cishell.reference.prefs.admin.internal.PrefAdminImpl"/> + <properties entry="OSGI-INF/algorithm.properties"/> + <reference name="LOG" interface="org.osgi.service.log.LogService"/> + <reference name="MTS" interface="org.osgi.service.metatype.MetaTypeService"/> + <reference name="CS" interface="org.osgi.service.cm.ConfigurationAdmin" + cardinality="1..1"/> + + <reference name="PREF_HOLDERS" + interface="org.cishell.framework.algorithm.AlgorithmFactory" + cardinality="0..n" + policy="dynamic" + bind="prefHolderRegistered" + unbind="prefHolderUnregistered" + /> + + <service> + <provide interface= + "org.cishell.reference.prefs.admin.PrefAdmin"/> + <provide interface= + "org.osgi.service.cm.ConfigurationPlugin"/> + <provide interface= + "org.osgi.service.cm.ConfigurationListener"/> + </service> +</component> + + + \ No newline at end of file Added: branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/l10n/bundle_en.properties =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/l10n/bundle_en.properties (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/l10n/bundle_en.properties 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,7 @@ +#Localization variables for OSGI-INF/metatatype/METADATA.XML +# +#Samples: +#input=Input +#desc=Enter an integer (that will be converted to a string) +#name=Input->String +#name_desc=Converts inputted integer to string Added: branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/metatype/METADATA.XML =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/metatype/METADATA.XML (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/OSGI-INF/metatype/METADATA.XML 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0"> + <Designate pid="org.cishell.reference.prefs.admin.PrefAdmin"> + <Object ocdref="org.cishell.reference.prefs.admin.PrefAdmin.OCD" /> + </Designate> +</metatype:MetaData> Added: branches/user_prefs/org.cishell.reference.prefs.admin/build.properties =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/build.properties (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/build.properties 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,5 @@ +source.. = src/ +output.. = build/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefAdmin.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefAdmin.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefAdmin.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,11 @@ +package org.cishell.reference.prefs.admin; + +import org.osgi.service.cm.ConfigurationPlugin; + +public interface PrefAdmin extends ConfigurationPlugin { + public PrefPage[] getLocalPrefPages(); + public PrefPage[] getGlobalPrefPages(); + public PrefPage[] getParamPrefPages(); + + public PrefsByService[] getPrefsByService(); +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPage.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPage.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPage.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,18 @@ +package org.cishell.reference.prefs.admin; + +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; + +public interface PrefPage { + + public static final int LOCAL = 0; + public static final int GLOBAL = 1; + public static final int PARAM = 2; + + public abstract ServiceReference getServiceReference(); + + public abstract int getType(); + + public abstract Configuration getPrefConf(); + public abstract PreferenceOCD getPrefOCD(); +} \ No newline at end of file Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPageComparator.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPageComparator.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefPageComparator.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,28 @@ +package org.cishell.reference.prefs.admin; + +import java.util.Comparator; + +import org.osgi.service.metatype.ObjectClassDefinition; + +public class PrefPageComparator implements Comparator { + public int compare (Object o1, Object o2) { + if (! (o1 instanceof PrefPage) || ! (o2 instanceof PrefPage)) { + throw new ClassCastException("Cannot compare two objects that are not both PrefPages."); + } + PrefPage pp1 = (PrefPage) o1; + ObjectClassDefinition ocd1 = pp1.getPrefOCD(); + if (ocd1 == null) { + return -1; + } + String ocd1Name = ocd1.getName(); + + PrefPage pp2 = (PrefPage) o2; + ObjectClassDefinition ocd2 = pp2.getPrefOCD(); + if (ocd2 == null) { + return 1; + } + String ocd2Name = ocd2.getName(); + + return ocd1Name.compareTo(ocd2Name); + } +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceAD.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceAD.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceAD.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,35 @@ +package org.cishell.reference.prefs.admin; + +import org.osgi.service.metatype.AttributeDefinition; + +public interface PreferenceAD extends AttributeDefinition { + + public static final int TEXT = 19; + public static final int DIRECTORY = 20; + public static final int FILE = 21; + public static final int FONT = 22; + public static final int PATH = 23; + public static final int CHOICE = 24; + public static final int COLOR = 25; + + public abstract int getCardinality(); + + public abstract String[] getDefaultValue(); + + public abstract String getDescription(); + + public abstract String getID(); + + public abstract String getName(); + + public abstract String[] getOptionLabels(); + + public abstract String[] getOptionValues(); + + public abstract int getType(); + + public abstract int getPreferenceType(); + + public abstract String validate(String value); + +} \ No newline at end of file Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceOCD.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceOCD.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PreferenceOCD.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,26 @@ +package org.cishell.reference.prefs.admin; + +import java.io.IOException; +import java.io.InputStream; + +import org.osgi.service.metatype.AttributeDefinition; +import org.osgi.service.metatype.ObjectClassDefinition; + +public interface PreferenceOCD extends ObjectClassDefinition { + + //use in standard way + public abstract AttributeDefinition[] getAttributeDefinitions(int filter); + + //use to get at the special preference attribute goodness. + public abstract PreferenceAD[] getPreferenceAttributeDefinitions( + int filter); + + public abstract String getDescription(); + + public abstract String getID(); + + public abstract InputStream getIcon(int size) throws IOException; + + public abstract String getName(); + +} \ No newline at end of file Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefsByService.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefsByService.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/PrefsByService.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,33 @@ +package org.cishell.reference.prefs.admin; + +import org.osgi.framework.ServiceReference; + +public class PrefsByService { + + private ServiceReference prefHolder; + private PrefPage[] localPrefs; + private PrefPage[] globalPrefs; + private PrefPage[] paramPrefs; + public PrefsByService(ServiceReference prefHolder, PrefPage[] localPrefs, PrefPage[] globalPrefs, PrefPage[] paramPrefs) { + this.prefHolder = prefHolder; + this.localPrefs = localPrefs; + this.globalPrefs = globalPrefs; + this.paramPrefs = paramPrefs; + } + + public ServiceReference getServiceReference() { + return this.prefHolder; + } + + public PrefPage[] getGlobalPrefPages() { + return this.globalPrefs; + } + + public PrefPage[] getLocalPrefPages() { + return this.localPrefs; + } + + public PrefPage[] getParamPrefPages() { + return this.paramPrefs; + } +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefAdminImpl.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefAdminImpl.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefAdminImpl.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,171 @@ +package org.cishell.reference.prefs.admin.internal; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.Enumeration; +import java.util.List; + +import org.cishell.framework.preference.PreferenceProperty; +import org.cishell.reference.prefs.admin.PrefAdmin; +import org.cishell.reference.prefs.admin.PrefPage; +import org.cishell.reference.prefs.admin.PrefsByService; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.osgi.service.cm.ConfigurationEvent; +import org.osgi.service.cm.ConfigurationListener; +import org.osgi.service.cm.ConfigurationPlugin; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.log.LogService; +import org.osgi.service.metatype.MetaTypeService; + +public class PrefAdminImpl implements PrefAdmin, ConfigurationPlugin, ConfigurationListener { + + private LogService log; + private MetaTypeService mts; + private ConfigurationAdmin ca; + + private PrefInfoGrabber prefInfoGrabber; + private PrefReferenceProcessor prefProcessor; + + private List prefReferencesToBeProcessed = new ArrayList(); + private List prefHolderReferences = new ArrayList(); + + private boolean hasBeenActivated = false; + + //PrefAdmin interface + + public PrefPage[] getLocalPrefPages() { + return prefProcessor.getAllLocalPrefPages(); + } + + + public PrefPage[] getGlobalPrefPages() { + return prefProcessor.getAllGlobalPrefPages(); + } + + public PrefPage[] getParamPrefPages() { + return prefProcessor.getAllParamPrefPages(); + } + + public PrefsByService[] getPrefsByService() { + return prefProcessor.getAllPrefsByService(); + } + + //Service Component Interface + + protected void activate(ComponentContext ctxt) { + + this.log = (LogService) ctxt.locateService("LOG"); + this.mts = (MetaTypeService) ctxt.locateService("MTS"); + this.ca = (ConfigurationAdmin) ctxt.locateService("CS"); + + this.prefInfoGrabber = new PrefInfoGrabber(log, mts, ca); + this.prefProcessor = new PrefReferenceProcessor(log, prefInfoGrabber); + + this.hasBeenActivated = true; + + //takes care of any prefHolders that may have been registered + //before the rest of these services were registered. + this.prefProcessor.processPrefReferences( + (ServiceReference[]) prefReferencesToBeProcessed.toArray(new ServiceReference[0])); + this.prefReferencesToBeProcessed.clear(); + } + + protected void deactivate(ComponentContext ctxt) { + } + + /** + * This method is called whenever a Service which potentially has preference + * information is registered. + * + * @param prefHolder The service reference for the service with preference information + */ + protected void prefHolderRegistered(ServiceReference prefHolder) { + this.prefReferencesToBeProcessed.add(prefHolder); + //(we must wait until this service is activated before we can properly process the preference holders) + if (this.hasBeenActivated == true) { + this.prefProcessor.processPrefReferences( + (ServiceReference[]) this.prefReferencesToBeProcessed.toArray(new ServiceReference[0])); + this.prefReferencesToBeProcessed.clear(); + } + + this.prefHolderReferences.add(prefHolder); + } + + protected void prefHolderUnregistered(ServiceReference prefHolder) { + this.prefHolderReferences.remove(prefHolder); + } + + // ConfigurationPlugin interface + + //injects global preferences into local preferences as they go from configuration service to the managed service + public void modifyConfiguration(ServiceReference reference, + Dictionary properties) { + PrefPage[] globalPrefPages = getGlobalPrefPages(); + for (int ii = 0; ii < globalPrefPages.length; ii++) { + PrefPage globalPrefPage = globalPrefPages[ii]; + Configuration globalPrefConf = globalPrefPage.getPrefConf(); + + String namespace = globalPrefConf.getPid(); + + Dictionary globalPrefDict = globalPrefConf.getProperties(); + + //the keys of each dictionary are the ids of global preference OCDs. + Enumeration ids = globalPrefDict.keys(); + while (ids.hasMoreElements()) { + String id = (String) ids.nextElement(); + String value = (String) globalPrefDict.get(id); + + String keyForConfiguration = namespace + "." + id; + + properties.put(keyForConfiguration, value); + } + } + } + + //ConfigurationListener interface + + public void configurationEvent(ConfigurationEvent event) { + if (event.getType() == ConfigurationEvent.CM_UPDATED) { + if (isFromGlobalConf(event.getPid())) { + sendGlobalPreferences(); + } else { + } + } else if (event.getType() == ConfigurationEvent.CM_DELETED) { + } + } + + /** + * Call when a global preference object is created or updated. + * Necessary because changes to global preference do not + * cause an update event for every ManagedService. + */ + private void sendGlobalPreferences() { + try { + for (int ii = 0; ii < this.prefHolderReferences.size(); ii++) { + ServiceReference prefHolder = (ServiceReference) this.prefHolderReferences.get(ii); + if (prefHolder.getProperty(PreferenceProperty.RECEIVE_PREFS_KEY) != null && + prefHolder.getProperty(PreferenceProperty.RECEIVE_PREFS_KEY).equals("true")) { + Configuration localPrefConf = ca.getConfiguration((String) prefHolder.getProperty(Constants.SERVICE_PID)); + try { + localPrefConf.update(); + } catch (IOException e) { + this.log.log(LogService.LOG_ERROR, "Unable to update configuration for " + localPrefConf.getPid(), e); + } + } + } + } catch (IOException e) { + this.log.log(LogService.LOG_ERROR, "Unable to obtain all configuration objects", e); + } + } + + private boolean isFromGlobalConf(String pid) { + return (pid.substring(0, pid.length() - 1).endsWith(PreferenceProperty.GLOBAL_PREFS_CONF_SUFFIX) + || pid.endsWith(PreferenceProperty.GLOBAL_PREFS_CONF_SUFFIX)); + } + + +} \ No newline at end of file Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefInfoGrabber.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefInfoGrabber.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefInfoGrabber.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,224 @@ +package org.cishell.reference.prefs.admin.internal; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; + +import org.cishell.framework.algorithm.AlgorithmProperty; +import org.cishell.framework.preference.PreferenceProperty; +import org.cishell.reference.prefs.admin.PreferenceOCD; +import org.osgi.framework.Bundle; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.osgi.service.log.LogService; +import org.osgi.service.metatype.MetaTypeInformation; +import org.osgi.service.metatype.MetaTypeService; +import org.osgi.service.metatype.ObjectClassDefinition; + +//NOTE: methods that get configuration objects don't really need the ObjectClassDefinitions. We just use them (hackishly) to know how many configuration objects we should get +//Since, there are possibly an infinite number of Configuration objects for a service, we need the ObjectClassDefinitions to tell us when to stop trying to grab configuration objects +//(If we try to grab one that didn't previously exist, it will just make an empty one, sometimes we want this behaviour and sometimes we don't). +public class PrefInfoGrabber implements PreferenceProperty, AlgorithmProperty { + + private LogService log; + private MetaTypeService mts; + private ConfigurationAdmin ca; + + public PrefInfoGrabber(LogService log, MetaTypeService mts, ConfigurationAdmin ca) { + this.log = log; + this.mts = mts; + this.ca = ca; + } + + public PreferenceOCD[] getLocalPrefOCDs(ServiceReference prefHolder) { + String localPrefOCDID = getLocalPrefOCDID(prefHolder); + return extractOCDs(prefHolder, localPrefOCDID); + } + + public PreferenceOCD[] getGlobalPrefOCDs(ServiceReference prefHolder) { + String globalPrefOCDID = getGlobalPrefOCDID(prefHolder); + return extractOCDs(prefHolder, globalPrefOCDID); + } + + public PreferenceOCD[] getParamPrefOCDs(ServiceReference prefHolder) { + String paramPrefOCDID = getParamPrefOCDID(prefHolder); + return extractParamOCDs(prefHolder, paramPrefOCDID); + } + + private String getLocalPrefOCDID(ServiceReference prefHolder) { + String localPrefOCDID = (String) prefHolder.getProperty(PreferenceProperty.LOCAL_PREF_PID); + if (localPrefOCDID != null) { + return localPrefOCDID; + } else { + //no name defined. Use default name. + String defaultLocalPrefOCDID = (String) prefHolder.getProperty(Constants.SERVICE_PID) + + PreferenceProperty.LOCAL_PREFS_OCD_SUFFIX; + return defaultLocalPrefOCDID; + } + } + + private String getGlobalPrefOCDID(ServiceReference prefHolder) { + String globalPrefOCDID = (String) prefHolder.getProperty(PreferenceProperty.GLOBAL_PREF_PID); + if (globalPrefOCDID != null) { + return globalPrefOCDID; + } else { + //no names defined. Use default names. + String defaultGlobalPrefOCDID = (String) prefHolder.getProperty(Constants.SERVICE_PID) + + PreferenceProperty.GLOBAL_PREFS_OCD_SUFFIX; + return defaultGlobalPrefOCDID; + } + } + + private String getParamPrefOCDID(ServiceReference prefHolder) { + System.out.println(" Getting param pref OCD ID for " + prefHolder.getProperty("service.pid")); + String paramPrefOCDID = (String) prefHolder.getProperty(AlgorithmProperty.PARAMETER_PID); + if ( paramPrefOCDID != null) { + System.out.println(" RETURN override: " + paramPrefOCDID); + return paramPrefOCDID; + } else { + //no names defined. Use default names. + String defaultParamPrefOCDID = (String) prefHolder.getProperty(Constants.SERVICE_PID) + PreferenceProperty.PARAM_PREFS_OCD_SUFFIX; + System.out.println(" RETURN default: " + defaultParamPrefOCDID); + return defaultParamPrefOCDID; + } + } + + private PreferenceOCD[] extractOCDs(ServiceReference prefHolder, String ocdID) { + Bundle bundle = prefHolder.getBundle(); + MetaTypeInformation bundleMetaTypeInfo = mts.getMetaTypeInformation(bundle); + List extractedOCDList = new ArrayList(); + //go through ocdID, ocdID + "2", ocdID + "3", etc..., until we try to get an OCD that doesn't exist. + PreferenceOCD extractedOCD = extractOCD(bundleMetaTypeInfo, ocdID); + int ii = 2; + while (extractedOCD != null) { + extractedOCDList.add(extractedOCD); + extractedOCD = extractOCD(bundleMetaTypeInfo, ocdID + ii); + ii++; + } + return (PreferenceOCD[]) extractedOCDList.toArray(new PreferenceOCD[extractedOCDList.size()]); + } + + //returns null if specified OCD does not exist. + private PreferenceOCD extractOCD(MetaTypeInformation bundleMetaTypeInfo, String ocdID) { + try { + ObjectClassDefinition requestedOCD = bundleMetaTypeInfo.getObjectClassDefinition(ocdID, null); + if (requestedOCD == null) return null; + PreferenceOCD wrappedOCD = new PreferenceOCDImpl(this.log, requestedOCD); + return wrappedOCD; + } catch (IllegalArgumentException e) { + //requested OCD does not exist + return null; + } + } + + //returning multiple OCDs just for the sake of symmetry, actually can only return 1 (or 0 on error) + private PreferenceOCD[] extractParamOCDs(ServiceReference prefHolder, String ocdID) { + Bundle bundle = prefHolder.getBundle(); + MetaTypeInformation bundleMetaTypeInfo = mts.getMetaTypeInformation(bundle); + PreferenceOCD paramOCD = extractOCD(bundleMetaTypeInfo, ocdID); + if (paramOCD != null) { + return new PreferenceOCD[]{paramOCD}; + } else { + return new PreferenceOCD[0]; + } + } + + public Configuration[] getLocalPrefConfs(ServiceReference prefHolder, PreferenceOCD[] prefOCDs) { + String localPrefConfID = getLocalPrefConfID(prefHolder); + return extractConfs(localPrefConfID, prefOCDs); + } + + public Configuration[] getGlobalPrefConfs(ServiceReference prefHolder, PreferenceOCD[] prefOCDs) { + String globalPrefConfID = getGlobalPrefConfID(prefHolder); + return extractConfs(globalPrefConfID, prefOCDs); + } + + public Configuration[] getParamPrefConfs(ServiceReference prefHolder, PreferenceOCD[] prefOCDs) { + String paramPrefConfID = getParamPrefConfID(prefHolder); + return extractConfs(paramPrefConfID, prefOCDs); + } + + public void ensurePrefsCanBeSentTo(ServiceReference serviceRef) { + try { + Configuration conf = ca.getConfiguration((String) serviceRef.getProperty(Constants.SERVICE_PID), null); + + Dictionary properties = conf.getProperties(); + if (properties == null) { + conf.update(new Hashtable()); + } + } catch (IOException e) { + return; + } + } + + + /** + * Gets all the configuration objects that correspond to the preference OCDs provided. + * @param confPID the PID that all the configuration objects start with, (confPID, confPID + "2", etc...) + * @param prefOCDs the OCDs associated with the configuration objects we are trying to get (puts a limit on # of Conf objects we try to extract) + * @return All the Configuration objects associated with the OCDs. + */ + private Configuration[] extractConfs(String confPID, PreferenceOCD[] prefOCDs) { + if (prefOCDs.length == 0) { + return new Configuration[0]; + } + List extractedConfList = new ArrayList(); + Configuration prefConf = extractConf(confPID); + if (prefConf != null) { + extractedConfList.add(prefConf); + for (int ii =2; ii < prefOCDs.length + 1; ii++) { + prefConf = extractConf(confPID + ii); + if (prefConf != null) { + extractedConfList.add(prefConf); + } else { + break; + } + } + } + + return (Configuration[]) extractedConfList.toArray(new Configuration[extractedConfList.size()]); + } + + //will return null on exception + private Configuration extractConf(String confPID) { + try { + return ca.getConfiguration(confPID, null); + } catch (IOException e) { + this.log.log(LogService.LOG_ERROR, "Unable to load configuration for " + confPID + " due to IO Error"); + return null; + } + } + + + private String getLocalPrefConfID(ServiceReference prefHolder) { + return (String) prefHolder.getProperty(Constants.SERVICE_PID); //has to be service.pid for configuration service to deliver updates to the right service + } + + private String getGlobalPrefConfID(ServiceReference prefHolder) { + String globalPrefOCDID = (String) prefHolder.getProperty(PreferenceProperty.GLOBAL_PREF_PID); + if (globalPrefOCDID != null) { + return globalPrefOCDID; + } else { + //no names defined. Use default names. + String defaultGlobalPrefOCDID = (String) prefHolder.getProperty(Constants.SERVICE_PID) + PreferenceProperty.GLOBAL_PREFS_CONF_SUFFIX; + return defaultGlobalPrefOCDID; + } + } + + private String getParamPrefConfID(ServiceReference prefHolder) { + String paramPrefOCDID = (String) prefHolder.getProperty(AlgorithmProperty.PARAMETER_PID); + if ( paramPrefOCDID != null) { + return paramPrefOCDID; + } else { + //no names defined. Use default names. + String defaultGlobalPrefOCDID = (String) prefHolder.getProperty(Constants.SERVICE_PID) + PreferenceProperty.PARAM_PREFS_CONF_SUFFIX; + return defaultGlobalPrefOCDID; + } + } + + +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefPageImpl.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefPageImpl.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefPageImpl.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,36 @@ +package org.cishell.reference.prefs.admin.internal; + +import org.cishell.reference.prefs.admin.PrefPage; +import org.cishell.reference.prefs.admin.PreferenceOCD; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; + +public class PrefPageImpl implements PrefPage { + private ServiceReference prefHolder; + private Configuration prefConf; + private PreferenceOCD prefOCD; + private int type; + + public PrefPageImpl(ServiceReference prefHolder,PreferenceOCD prefOCD, Configuration prefConf, int type) { + this.prefHolder = prefHolder; + this.prefOCD = prefOCD; + this.prefConf = prefConf; + this.type = type; + } + + public ServiceReference getServiceReference() { + return this.prefHolder; + } + + public int getType() { + return this.type; + } + + public PreferenceOCD getPrefOCD() { + return this.prefOCD; + } + + public Configuration getPrefConf() { + return this.prefConf; + } +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefReferenceProcessor.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefReferenceProcessor.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PrefReferenceProcessor.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,288 @@ +package org.cishell.reference.prefs.admin.internal; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; + +import org.cishell.framework.preference.PreferenceProperty; +import org.cishell.reference.prefs.admin.PrefPage; +import org.cishell.reference.prefs.admin.PreferenceAD; +import org.cishell.reference.prefs.admin.PreferenceOCD; +import org.cishell.reference.prefs.admin.PrefsByService; +import org.osgi.framework.Bundle; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; +import org.osgi.service.log.LogService; +import org.osgi.service.metatype.AttributeDefinition; +import org.osgi.service.metatype.ObjectClassDefinition; + +public class PrefReferenceProcessor{ + + private LogService log; + + private PrefInfoGrabber prefInfoGrabber; + + private List allLocalPrefPages = new ArrayList(); + private List allGlobalPrefPages = new ArrayList(); + private List allParamPrefPages = new ArrayList(); + + private List allPrefsByService = new ArrayList(); + + public PrefReferenceProcessor(LogService log, PrefInfoGrabber prefInfoGrabber) { + this.log = log; + + this.prefInfoGrabber = prefInfoGrabber; + } + + public void processPrefReferences(ServiceReference[] prefReferences) { + + //for each service that purports to hold preference information... + for (int ii = 0; ii < prefReferences.length; ii++) { + ServiceReference prefReference = prefReferences[ii]; + System.out.println("Processing " + prefReference.getProperty("service.pid")); + //get all preference pages from this service by type, and save them by type + + PrefPage[] localPrefPages = null; + if(isTurnedOn(prefReference, PreferenceProperty.PUBLISH_LOCAL_PREF_VALUE)) { + localPrefPages = getLocalPrefPages(prefReference); + initializeConfigurations(localPrefPages); + this.allLocalPrefPages.addAll(Arrays.asList(localPrefPages)); + warnIfReceivePrefsIsNotOn(prefReference); + } + + PrefPage[] globalPrefPages = null; + if (isTurnedOn(prefReference, PreferenceProperty.PUBLISH_GLOBAL_PREF_VALUE)) { + globalPrefPages = getGlobalPrefPages(prefReference); + initializeConfigurations(globalPrefPages); + this.allGlobalPrefPages.addAll(Arrays.asList(globalPrefPages)); + } + + PrefPage[] paramPrefPages = null; + if (isTurnedOn(prefReference,PreferenceProperty.PUBLISH_PARAM_DEFAULT_PREF_VALUE)) { + System.out.println(" Attempting to publish param default prefs for " + prefReference.getProperty("service.pid")); + paramPrefPages = getParamPrefPages(prefReference); + initializeConfigurations(paramPrefPages); + this.allParamPrefPages.addAll(Arrays.asList(paramPrefPages)); + } + + //save all the preferences by service as well + + PrefsByService allPrefsForThisService = new PrefsByService(prefReference, localPrefPages, globalPrefPages, paramPrefPages); + this.allPrefsByService.add(allPrefsForThisService); + + //make sure that preferences are sent to this service if it wants to see them. + + if (isTurnedOn(prefReference, PreferenceProperty.RECEIVE_PREFS_KEY)) { + prefInfoGrabber.ensurePrefsCanBeSentTo(prefReference); + } + } + } + + public PrefPage[] getAllLocalPrefPages() { + return getPrefPages(allLocalPrefPages); + } + + public PrefPage[] getAllGlobalPrefPages() { + return getPrefPages(allGlobalPrefPages); + } + + public PrefPage[] getAllParamPrefPages() { + return getPrefPages(allParamPrefPages); + } + + public PrefsByService[] getAllPrefsByService() { + return (PrefsByService[]) allPrefsByService.toArray(new PrefsByService[allPrefsByService.size()]); + } + + private PrefPage[] getLocalPrefPages(ServiceReference prefHolder) { + PreferenceOCD[] localPrefOCDs = prefInfoGrabber.getLocalPrefOCDs(prefHolder); + Configuration[] localPrefConfs = prefInfoGrabber.getLocalPrefConfs(prefHolder, localPrefOCDs); + PrefPage[] localPrefPages = composePrefPages(prefHolder, localPrefOCDs, localPrefConfs, PrefPage.LOCAL); + return localPrefPages; + } + + private PrefPage[] getGlobalPrefPages(ServiceReference prefHolder) { + PreferenceOCD[] globalPrefOCDs = prefInfoGrabber.getGlobalPrefOCDs(prefHolder); + Configuration[] globalPrefConfs = prefInfoGrabber.getGlobalPrefConfs(prefHolder, globalPrefOCDs); + PrefPage[] globalPrefPages = composePrefPages(prefHolder, globalPrefOCDs, globalPrefConfs, PrefPage.GLOBAL); + return globalPrefPages; + } + + private PrefPage[] getParamPrefPages(ServiceReference prefHolder) { + PreferenceOCD[] paramPrefOCDs = prefInfoGrabber.getParamPrefOCDs(prefHolder); + Configuration[] paramPrefConfs = prefInfoGrabber.getParamPrefConfs(prefHolder, paramPrefOCDs); + PrefPage[] paramPrefPages = composePrefPages(prefHolder, paramPrefOCDs, paramPrefConfs, PrefPage.PARAM); + return paramPrefPages; + } + + //prefOCD.length should == prefConfs.length + private PrefPage[] composePrefPages(ServiceReference prefHolder, PreferenceOCD[] prefOCDs, Configuration[] prefConfs, int type) { + int minLength = Math.min(prefOCDs.length, prefConfs.length); + List composedPrefPageList = new ArrayList(prefOCDs.length); + for (int ii = 0; ii < minLength; ii++) { + PrefPage composedPrefPage = new PrefPageImpl(prefHolder, prefOCDs[ii], prefConfs[ii], type); + composedPrefPageList.add(composedPrefPage); + } + + return (PrefPage[]) composedPrefPageList.toArray(new PrefPage[composedPrefPageList.size()]); + } + + private void initializeConfiguration(PrefPage prefPage) { + Configuration prefConf = prefPage.getPrefConf(); + Dictionary prefDict = prefConf.getProperties(); + PreferenceOCD prefOCD = prefPage.getPrefOCD(); + + //if there are no properties defined for this prefPages configuration... + if (prefDict == null || bundleHasBeenUpdated(prefPage)) { + + //create configuration properties for this pref based on the default values in its OCD. + prefDict = new Hashtable(); + + PreferenceAD[] prefADs = prefOCD.getPreferenceAttributeDefinitions(ObjectClassDefinition.ALL); + for (int ii = 0; ii < prefADs.length; ii++) { + AttributeDefinition prefAD = prefADs[ii]; + + + String id = prefAD.getID(); + String val = prefAD.getDefaultValue()[0]; + + try { + prefDict.put(id, val); + } catch (Throwable e) {; + e.printStackTrace(); + } + + } + + prefDict.put(PreferenceProperty.BUNDLE_VERSION_KEY, getCurrentBundleVersion(prefPage)); + + try { + prefConf.update(prefDict); + } catch (IOException e) { + this.log.log(LogService.LOG_ERROR, "Unable to update configuration with PID " + prefConf.getPid(), e); + } + } else { + //update it anyway, because if it is a global conf it needs to be propogated. + //(if it is a global preference, this update will be caught by our ConfigurationListener, + //and the global preference data will be propagated. + try { + prefConf.update(prefDict); + } catch (IOException e) { + this.log.log(LogService.LOG_ERROR, "Unable to update configuration with PID " + prefConf.getPid(), e); + } + } + + //TODO: does not worry about old version of bundles for now + } + + private void initializeConfigurations(PrefPage[] prefPages) + { + for (int ii = 0; ii < prefPages.length; ii++) { + PrefPage prefPage = prefPages[ii]; + initializeConfiguration(prefPage); + } + } + + //only supports 3 publish keys and receive_prefs key + private boolean isTurnedOn(ServiceReference prefReference, String processingKey) { + if (processingKey.equals(PreferenceProperty.RECEIVE_PREFS_KEY)) { + String receivePrefsValue = (String) prefReference.getProperty(PreferenceProperty.RECEIVE_PREFS_KEY); + return receivePrefsValue != null && receivePrefsValue.equals("true"); + } else { + String unparsedPublishedPrefsValues = (String) prefReference.getProperty(PreferenceProperty.PREFS_PUBLISHED_KEY); + if (unparsedPublishedPrefsValues == null) { + if (processingKey ==PreferenceProperty.PUBLISH_PARAM_DEFAULT_PREF_VALUE) { + return true; + } else { + return false; + } + } + + String[] publishedPrefsValues = unparsedPublishedPrefsValues.split(","); + for (int ii = 0; ii < publishedPrefsValues.length; ii++) { + if (publishedPrefsValues[ii].equals(processingKey)) { + return true; + } + } + + //makes it so parameter prefs are published by default + if (publishedPrefsValues.length == 0 && processingKey ==PreferenceProperty.PUBLISH_PARAM_DEFAULT_PREF_VALUE) { + return true; + } + + return false; + } + } + + private boolean bundleHasBeenUpdated(PrefPage prefPage) { + String currentBundleVersion = getCurrentBundleVersion(prefPage); + String savedBundleVersion = getSavedBundleVersion(prefPage); + + if (savedBundleVersion == null) { + return false; + } + + if (currentBundleVersion.equals(savedBundleVersion)) { + return false; + } else { + logBundleWasUpdated(prefPage); + return true; + } + } + + private String getCurrentBundleVersion(PrefPage prefPage) { + Bundle b = prefPage.getServiceReference().getBundle(); + String currentBundleVersion = (String) b.getHeaders().get(PreferenceProperty.BUNDLE_VERSION_KEY); + return currentBundleVersion; + } + + private String getSavedBundleVersion(PrefPage prefPage) { + Dictionary prefDict = prefPage.getPrefConf().getProperties(); + if (prefDict == null) { + return null; + } + //no namespace in front of bundle version + String bundleVersionForLocalsAndParams = (String) prefDict.get(PreferenceProperty.BUNDLE_VERSION_KEY); + if (bundleVersionForLocalsAndParams != null) { + return bundleVersionForLocalsAndParams; + } else { + //try global kind, with namespace in front + String servicePID = (String) prefPage.getServiceReference().getProperty(Constants.SERVICE_PID); + String bundleVersionForGlobals = (String) prefDict.get(servicePID + "." + PreferenceProperty.BUNDLE_VERSION_KEY); + + if (bundleVersionForGlobals != null) { + return bundleVersionForGlobals; + } else { + return null; + } + } + } + + private void logBundleWasUpdated(PrefPage prefPage) { + String servicePID = (String) prefPage.getServiceReference().getProperty(Constants.SERVICE_PID); + String prefOCDName = prefPage.getPrefOCD().getName(); + this.log.log(LogService.LOG_WARNING, "The Bundle for the service " + servicePID + + " was updated, so the preferences for " + prefOCDName + + " will be set to the new defaults."); + } + + private PrefPage[] getPrefPages(List prefPageList) { + return (PrefPage[]) prefPageList.toArray(new PrefPage[prefPageList.size()]); + } + + private void warnIfReceivePrefsIsNotOn(ServiceReference prefHolder) { + if (! isTurnedOn(prefHolder, PreferenceProperty.RECEIVE_PREFS_KEY)) { + String servicePID = (String) prefHolder.getProperty(Constants.SERVICE_PID); + log.log(LogService.LOG_WARNING, "Algorithm Developer Error: \r\n" + + "The algorithm " + servicePID + " has published local preferences without requested to receive preferences. \r\n" + + " Algorithms that want to see preferences (both local and global) " + + "need to set receive_prefs=true in their .properties file, but since there is no purpose in defining local preferences " + + "without being able to receive preferences, we will turn it on for you. You must also implement the ManagedService" + + "interface for the AlgorithmFactory if you have not already done so."); + } + } +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceADImpl.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceADImpl.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceADImpl.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,205 @@ +package org.cishell.reference.prefs.admin.internal; + +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; + +import org.cishell.reference.prefs.admin.PreferenceAD; +import org.osgi.service.log.LogService; +import org.osgi.service.metatype.AttributeDefinition; + +public class PreferenceADImpl implements AttributeDefinition, PreferenceAD { + + private static final String URI_FILE_PREFIX = "file:"; + + private LogService log; + + private AttributeDefinition realAD; + + private int preferenceType; + private String[] interpretedDefaultValue; + + private String platformIndepInstallDirPath; + + public PreferenceADImpl(LogService log, AttributeDefinition realAD) { + this.log = log; + + this.realAD = realAD; + + this.preferenceType = inferPreferenceType(realAD); + + this.platformIndepInstallDirPath = generateIndepInstallDirPath(); + this.interpretedDefaultValue = interpretDefaultValue(this.realAD.getDefaultValue()); + } + + private int inferPreferenceType(AttributeDefinition realAD) { + int preferenceType; + if (realAD.getType() == AttributeDefinition.STRING) { + String defaultVal = realAD.getDefaultValue()[0]; + + if (defaultVal.startsWith(TypePrefixes.DIRECTORY_PREFIX)) { + preferenceType = DIRECTORY; + } else if (defaultVal.startsWith(TypePrefixes.FILE_PREFIX)) { + preferenceType = FILE; + } else if (defaultVal.startsWith(TypePrefixes.FONT_PREFIX)) { + preferenceType = FONT; + } else if (defaultVal.startsWith(TypePrefixes.PATH_PREFIX)) { + preferenceType = PATH; + } else if (realAD.getOptionLabels() != null) { + preferenceType = CHOICE; + } else if (defaultVal.startsWith(TypePrefixes.COLOR_PREFIX)) { + preferenceType = COLOR; + } else { + preferenceType = TEXT; + } + } else { + preferenceType = realAD.getType(); + } + + return preferenceType; + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getCardinality() + */ + public int getCardinality() { + return this.realAD.getCardinality(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getDefaultValue() + */ + public String[] getDefaultValue() { + + return interpretedDefaultValue; + } + + private String generateIndepInstallDirPath() { + String installDirPath = System.getProperty("osgi.install.area").replace(URI_FILE_PREFIX, ""); + File installDirFile = new File(installDirPath); + URI platformIndependentFile = installDirFile.toURI(); + String platformIndepInstallDirPath = platformIndependentFile.toString(); + return platformIndepInstallDirPath; + } + + private String[] interpretDefaultValue(String[] rawDefaultValues) { + String[] interpretedDefaultValues = new String[rawDefaultValues.length]; + for (int i = 0; i < rawDefaultValues.length; i++) { + String rawDefaultValue = rawDefaultValues[i]; + + interpretedDefaultValues[i] = interpretDefaultValue(rawDefaultValue); + } + + return interpretedDefaultValues; + } + private String interpretDefaultValue(String rawDefaultValue) { + int preferenceType = getPreferenceType(); + + if (preferenceType == DIRECTORY) { + String uriFormattedDefaultValue = rawDefaultValue.replace(TypePrefixes.DIRECTORY_PREFIX, URI_FILE_PREFIX); + return makePlatformSpecificPath(uriFormattedDefaultValue); + } else if (preferenceType == FILE) { + if (rawDefaultValue.equals("file:")) { + //allows empty values + return ""; + } + String uriFormattedDefaultValue = rawDefaultValue; //already in URI form, semi-coincidentally + return makePlatformSpecificPath(uriFormattedDefaultValue); + } else if (preferenceType == FONT) { + return rawDefaultValue.replace(TypePrefixes.FONT_PREFIX, ""); + } else if (preferenceType == PATH) { + return rawDefaultValue.replace(TypePrefixes.PATH_PREFIX, ""); + } else if (preferenceType == TEXT) { + return rawDefaultValue; + } else if (preferenceType == COLOR) { + return rawDefaultValue.replace(TypePrefixes.COLOR_PREFIX, ""); + }else { + return rawDefaultValue; + } + } + + private String makePlatformSpecificPath(String platformIndependentPath) { + //if the original platformIndependentPath is relative, stick the home directory on to it. + + if (! platformIndependentPath.startsWith("file:/")) { + //it's a relative path + //make it absolute + platformIndependentPath = platformIndepInstallDirPath + platformIndependentPath.replace(URI_FILE_PREFIX, ""); + } + //make the whole platformIndependentPath platform specific + + try { + URI uriInterpretation = new URI(platformIndependentPath); + File platformSpecificInterpretation = new File(uriInterpretation); + + /* + * may need to change to canonical path at some point, but that throws an IOException + * (and may cause performance problems), so skipping for now + */ + String platformSpecificDirectory = platformSpecificInterpretation.getAbsolutePath(); + return platformSpecificDirectory; + } catch (URISyntaxException e) { + this.log.log(LogService.LOG_WARNING, "Invalid syntax in preference AD " + realAD.getName()); + return System.getProperty("osgi.install.area").replace(URI_FILE_PREFIX, ""); + } + + //return it + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getDescription() + */ + public String getDescription() { + return this.realAD.getDescription(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getID() + */ + public String getID() { + return this.realAD.getID(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getName() + */ + public String getName() { + return this.realAD.getName(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getOptionLabels() + */ + public String[] getOptionLabels() { + return this.realAD.getOptionLabels(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getOptionValues() + */ + public String[] getOptionValues() { + return this.realAD.getOptionValues(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getType() + */ + public int getType() { + return this.realAD.getType(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#getPreferenceType() + */ + public int getPreferenceType() { + return this.preferenceType; + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceAttributeDefinition#validate(java.lang.String) + */ + public String validate(String value) { + return this.realAD.validate(value); + } + +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceOCDImpl.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceOCDImpl.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/PreferenceOCDImpl.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,93 @@ +package org.cishell.reference.prefs.admin.internal; + +import java.io.IOException; +import java.io.InputStream; + +import org.cishell.reference.prefs.admin.PreferenceAD; +import org.cishell.reference.prefs.admin.PreferenceOCD; +import org.osgi.service.log.LogService; +import org.osgi.service.metatype.AttributeDefinition; +import org.osgi.service.metatype.ObjectClassDefinition; + +public class PreferenceOCDImpl implements ObjectClassDefinition, PreferenceOCD { + + private ObjectClassDefinition realOCD; + private PreferenceAD[] allWrappedADs; + private PreferenceAD[] optionalWrappedADs; + private PreferenceAD[] requiredWrappedADs; + + private LogService log; + + public PreferenceOCDImpl(LogService log, ObjectClassDefinition realOCD) { + this.log = log; + this.realOCD = realOCD; + //TODO: don't always return all attributeDefinitions, regardless of filter + this.allWrappedADs = wrapAttributeDefinitions(realOCD.getAttributeDefinitions(ObjectClassDefinition.ALL)); + this.optionalWrappedADs = wrapAttributeDefinitions(realOCD.getAttributeDefinitions(ObjectClassDefinition.OPTIONAL)); + this.requiredWrappedADs = wrapAttributeDefinitions(realOCD.getAttributeDefinitions(ObjectClassDefinition.REQUIRED)); + } + + private PreferenceAD[] wrapAttributeDefinitions(AttributeDefinition[] realAttributeDefinitions) { + PreferenceAD[] wrappedADs = new PreferenceAD[realAttributeDefinitions.length]; + + for (int i = 0; i < realAttributeDefinitions.length; i++) { + AttributeDefinition realAD = realAttributeDefinitions[i]; + PreferenceAD wrappedAD = new PreferenceADImpl(this.log, realAD); + + wrappedADs[i] = wrappedAD; + } + + return wrappedADs; + } + + + //use in standard way + public AttributeDefinition[] getAttributeDefinitions(int filter) { + return this.realOCD.getAttributeDefinitions(filter); + + } + + //use to get at the special preference attribute goodness. + public PreferenceAD[] getPreferenceAttributeDefinitions(int filter) { + if (filter == ObjectClassDefinition.ALL) { + return this.allWrappedADs; + } else if (filter == ObjectClassDefinition.OPTIONAL) { + return this.optionalWrappedADs; + } else if (filter == ObjectClassDefinition.REQUIRED) { + return this.requiredWrappedADs; + } else { + log.log(LogService.LOG_WARNING, + "Programmer Error: attempted to get preference attribute definitions with invalid filter " + filter); + return new PreferenceAD[0]; + } + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceObjectClassDefinition#getDescription() + */ + public String getDescription() { + return this.realOCD.getDescription(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceObjectClassDefinition#getID() + */ + public String getID() { + return this.realOCD.getID(); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceObjectClassDefinition#getIcon(int) + */ + public InputStream getIcon(int size) throws IOException { + return this.realOCD.getIcon(size); + } + + /* (non-Javadoc) + * @see org.cishell.service.prefadmin.shouldbeelsewhere.PreferenceObjectClassDefinition#getName() + */ + public String getName() { + return this.realOCD.getName(); + } + +} Added: branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/TypePrefixes.java =================================================================== --- branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/TypePrefixes.java (rev 0) +++ branches/user_prefs/org.cishell.reference.prefs.admin/src/org/cishell/reference/prefs/admin/internal/TypePrefixes.java 2008-03-21 16:20:18 UTC (rev 668) @@ -0,0 +1,17 @@ +package org.cishell.reference.prefs.admin.internal; + +import org.osgi.service.metatype.AttributeDefinition; + +public class TypePrefixes { + + public static final String FONT_PREFIX = "font:"; + public static final String DIRECTORY_PREFIX = "directory:"; + public static final String FILE_PREFIX = "file:"; + public static final String PATH_PREFIX = "path:"; + public static final String COLOR_PREFIX = "color:"; + + + public static boolean hasPrefix(AttributeDefinition prefAD, String prefix) { + return prefAD.getDefaultValue()[0... [truncated message content] |