diff options
| author | abranson <andrew.branson@cern.ch> | 2011-08-04 00:42:34 +0200 |
|---|---|---|
| committer | abranson <andrew.branson@cern.ch> | 2011-08-04 00:42:34 +0200 |
| commit | 0ec8481c10cd8277d84c7c1a785483a0a739e5a0 (patch) | |
| tree | 5f6e5d9ae75193e67e6f3b3dfa488960c5cde1d5 /source/com/c2kernel/utils | |
| parent | 036cbdba66f804743c4c838ed598d6972c4b3e17 (diff) | |
More code cleanup:
Refactored Entity Proxy Subscription to handle generics better
Rewrote RemoteMap to use TreeMap instead of the internal array for
order. It now sorts its keys by number if they parse, else as strings.
Removed a no-longer-in-progress outcome form class
Diffstat (limited to 'source/com/c2kernel/utils')
19 files changed, 257 insertions, 211 deletions
diff --git a/source/com/c2kernel/utils/ActDefCache.java b/source/com/c2kernel/utils/ActDefCache.java index 148d934..2982aa3 100644 --- a/source/com/c2kernel/utils/ActDefCache.java +++ b/source/com/c2kernel/utils/ActDefCache.java @@ -1,26 +1,26 @@ /**
- *
+ *
*/
package com.c2kernel.utils;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectNotFoundException;
-import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.proxy.EntityProxyObserver;
import com.c2kernel.entity.proxy.ItemProxy;
+import com.c2kernel.entity.proxy.MemberSubscription;
import com.c2kernel.lifecycle.ActivityDef;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.outcome.Viewpoint;
public class ActDefCache {
-
+
SoftCache<String, ActCacheEntry> actCache = new SoftCache<String, ActCacheEntry>();
-
+
public ActivityDef get(String actName, String actVersion) throws ObjectNotFoundException, InvalidDataException {
ActivityDef thisActDef;
synchronized(actCache) {
- ActCacheEntry thisActDefEntry = ((ActCacheEntry)actCache.get(actName+"_"+actVersion));
+ ActCacheEntry thisActDefEntry = actCache.get(actName+"_"+actVersion);
if (thisActDefEntry == null) {
Logger.msg(6, actName+" v"+actVersion+" not found in cache. Retrieving.");
ItemProxy actDefItem = LocalObjectLoader.loadLocalObjectDef("/desc/ActivityDesc/", actName);
@@ -50,7 +50,7 @@ public class ActDefCache { }
return thisActDef;
}
-
+
public void removeAct(String id) {
synchronized(actCache) {
if (actCache.keySet().contains(id)) {
@@ -60,7 +60,7 @@ public class ActDefCache { }
}
- public class ActCacheEntry implements EntityProxyObserver {
+ public class ActCacheEntry implements EntityProxyObserver<Viewpoint> {
public String id;
public ItemProxy actProxy;
public ActivityDef actDef;
@@ -70,22 +70,31 @@ public class ActDefCache { this.actDef = actDef;
this.parent = parent;
this.actProxy = actProxy;
- actProxy.subscribe(this, ClusterStorage.VIEWPOINT, false);
+ actProxy.subscribe(new MemberSubscription<Viewpoint>(this, ClusterStorage.VIEWPOINT, false));
}
+ @Override
public void finalize() {
parent.removeAct(id);
actProxy.unsubscribe(this);
}
- public void add(C2KLocalObject contents) {
+ @Override
+ public void add(Viewpoint contents) {
parent.removeAct(id);
}
-
+
+ @Override
public void remove(String id) {
parent.removeAct(id);
}
-
+
+ @Override
public String toString() {
- return "ActDef cache entry: "+id;
+ return "ActDef cache entry: "+id;
+ }
+ @Override
+ public void control(String control, String msg) {
+ // TODO Auto-generated method stub
+
}
}
}
\ No newline at end of file diff --git a/source/com/c2kernel/utils/CastorArrayList.java b/source/com/c2kernel/utils/CastorArrayList.java index 7596dd9..ed15c0f 100644 --- a/source/com/c2kernel/utils/CastorArrayList.java +++ b/source/com/c2kernel/utils/CastorArrayList.java @@ -5,7 +5,7 @@ import java.util.ArrayList; /**************************************************************************
* Wrapper for a root element to an ArrayList. Castor Marshalls arraylists
- * as multiple elements, so this class is needed to provide a root element
+ * as multiple elements, so this class is needed to provide a root element
* to stop it crashing.
*
* $Revision: 1.4 $
@@ -17,12 +17,12 @@ import java.util.ArrayList; //
abstract public class CastorArrayList<T> implements Serializable{
public ArrayList<T> list;
-
+
public CastorArrayList() {
super();
- list = new ArrayList<T>();
+ list = new ArrayList<T>();
}
-
+
public CastorArrayList(ArrayList<T> list) {
this();
this.list = list;
diff --git a/source/com/c2kernel/utils/CastorXMLUtility.java b/source/com/c2kernel/utils/CastorXMLUtility.java index 1f68e77..4dca391 100644 --- a/source/com/c2kernel/utils/CastorXMLUtility.java +++ b/source/com/c2kernel/utils/CastorXMLUtility.java @@ -21,7 +21,7 @@ import com.c2kernel.persistency.outcome.Outcome; /**************************************************************************
* Loads all mapfiles, and wraps marshalling/unmarshalling
- *
+ *
* @author $Author: abranson $ $Date: 2004/10/20 14:10:21 $
* @version $Revision: 1.12 $
**************************************************************************/
@@ -33,7 +33,7 @@ public class CastorXMLUtility /**
* Looks for a file called 'index.xml' at the given URL, and loads every file
* listed in there by relative path
- *
+ *
* @param mapURL - map root
*/
static public void loadMapsFrom(URL mapURL) throws InvalidDataException {
@@ -46,7 +46,7 @@ public class CastorXMLUtility Logger.warning("Could not load map index from "+mapURL.toString());
return;
}
-
+
StringTokenizer sTokenizer = new StringTokenizer(index);
while( sTokenizer.hasMoreTokens() ) {
String thisMap = sTokenizer.nextToken();
@@ -55,11 +55,11 @@ public class CastorXMLUtility } catch (Exception e) {
Logger.error(e);
throw new InvalidDataException("Error loading map '"+thisMap+"'", "");
- }
+ }
}
Logger.msg("Loaded all maps from "+mapURL.toString());
}
-
+
/**************************************************************************
* Updates a mapping referenced by the mapID.
* The same mapping cannot be loaded many times as it generates an exception.
diff --git a/source/com/c2kernel/utils/DateUtility.java b/source/com/c2kernel/utils/DateUtility.java index 6e3b569..bc9fb05 100755..100644 --- a/source/com/c2kernel/utils/DateUtility.java +++ b/source/com/c2kernel/utils/DateUtility.java @@ -39,7 +39,7 @@ public class DateUtility time.append(timeStamp.mSecond);
return time.toString();
}
-
+
public static int getNbDayInYear(GTimeStamp date)
{
int centuary = date.mYear / 100;
@@ -78,7 +78,7 @@ public class DateUtility return 31;
}
}
-
+
public static long diff(GTimeStamp date1, GTimeStamp date2)
{
GTimeStamp tmp = new GTimeStamp(date1.mYear, date1.mMonth, date1.mDay, date1.mHour, date1.mMinute, date1.mSecond, date1.mTimeOffset);
@@ -114,5 +114,5 @@ public class DateUtility tmp.mDay = tmp.mDay + getNbDayInMonth(tmp);
}
return (((tmp.mDay - date2.mDay) * 24 + tmp.mHour - date2.mHour) * 60 + tmp.mMinute - date2.mMinute) * 60 + tmp.mSecond - date2.mSecond;
- }
+ }
}
diff --git a/source/com/c2kernel/utils/Dom4JElementParser.java b/source/com/c2kernel/utils/Dom4JElementParser.java index a4d5bbe..e3601e5 100755..100644 --- a/source/com/c2kernel/utils/Dom4JElementParser.java +++ b/source/com/c2kernel/utils/Dom4JElementParser.java @@ -16,14 +16,14 @@ public class Dom4JElementParser try
{
SAXReader reader = new SAXReader();
-
+
Document d = reader.read(new StringReader(data));
- List list = d.selectNodes( xpath );
+ List<?> list = d.selectNodes( xpath );
String[] returnArray = new String[list.size()];
int i=0;
- for ( Iterator iter = list.iterator(); iter.hasNext();i++ )
- {
- Object object = iter.next();
+ for ( Iterator<?> iter = list.iterator(); iter.hasNext();i++ )
+ {
+ Object object = iter.next();
if (object instanceof Element) returnArray[i]=((Element)object).getText();
else if (object instanceof Attribute) returnArray[i]=((Attribute)object).getText();
}
diff --git a/source/com/c2kernel/utils/FileStringUtility.java b/source/com/c2kernel/utils/FileStringUtility.java index 84f46ca..843a44e 100644 --- a/source/com/c2kernel/utils/FileStringUtility.java +++ b/source/com/c2kernel/utils/FileStringUtility.java @@ -1,7 +1,16 @@ package com.c2kernel.utils;
//Java
-import java.io.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
@@ -73,7 +82,7 @@ public class FileStringUtility lines.addElement(thisLine);
String[] lineArray = new String[lines.size()];
for (int i = 0; i < lines.size(); i++)
- lineArray[i] = (String) lines.get(i);
+ lineArray[i] = lines.get(i);
return lineArray;
}
@@ -106,7 +115,7 @@ public class FileStringUtility **************************************************************************/
static public void string2File(String fileName, String data) throws FileNotFoundException, IOException
{
- string2File(new File(fileName), data);
+ string2File(new File(fileName), data);
}
/**************************************************************************
@@ -226,8 +235,8 @@ public class FileStringUtility {
files = dir.listFiles();
- for (int i = 0; i < files.length; i++)
- deleteDir(files[i].getAbsolutePath(), true, true);
+ for (File file : files)
+ deleteDir(file.getAbsolutePath(), true, true);
}
return deleteDir(dirPath);
@@ -255,11 +264,10 @@ public class FileStringUtility files = dir.listFiles();
- for (int i = 0; i < files.length; i++)
- {
- fileName = files[i].getName();
+ for (File file : files) {
+ fileName = file.getName();
- if (files[i].isFile())
+ if (file.isFile())
{
fileNames.add(dirPath + "/" + fileName);
}
@@ -334,7 +342,7 @@ public class FileStringUtility /**************************************************************************
* Load the contents of the language file
* *************************************************************************/
- static public Hashtable loadLanguageFile(String configFile)
+ static public Hashtable<String, String> loadLanguageFile(String configFile)
{
try
{
@@ -353,7 +361,7 @@ public class FileStringUtility {
Logger.error("FileStringUtility.loadLanguageFile() - could not load language file '" + configFile+"'");
Logger.error(e);
- return new Hashtable();
+ return new Hashtable<String, String>();
}
}
@@ -402,7 +410,7 @@ public class FileStringUtility StringBuffer tmp=new StringBuffer(src);
int length = from.length();
int index = tmp.toString().indexOf(from);
- while (index>0)
+ while (index>0)
{
tmp.replace(index,index+length,to);
index = tmp.toString().indexOf(from);
diff --git a/source/com/c2kernel/utils/GTimeStampComparator.java b/source/com/c2kernel/utils/GTimeStampComparator.java index f57f20d..3680a50 100755..100644 --- a/source/com/c2kernel/utils/GTimeStampComparator.java +++ b/source/com/c2kernel/utils/GTimeStampComparator.java @@ -4,23 +4,24 @@ import java.util.Comparator; import com.c2kernel.common.GTimeStamp;
-public class GTimeStampComparator implements Comparator {
+public class GTimeStampComparator implements Comparator<Object> {
+ @Override
public int compare(Object arg0, Object arg1) {
GTimeStamp t0 = (GTimeStamp)arg0;
GTimeStamp t1 = (GTimeStamp)arg1;
-
+
int retVal = compareInt(t0.mYear, t1.mYear);
if (retVal == 0) retVal = compareInt(t0.mMonth, t1.mMonth);
if (retVal == 0) retVal = compareInt(t0.mDay, t1.mDay);
if (retVal == 0) retVal = compareInt(t0.mHour-(t0.mTimeOffset/3600), t1.mHour-(t1.mTimeOffset/3600));
if (retVal == 0) retVal = compareInt(t0.mMinute, t1.mMinute);
if (retVal == 0) retVal = compareInt(t0.mSecond, t1.mSecond);
-
+
return retVal;
}
- private int compareInt(int i, int j) {
+ private static int compareInt(int i, int j) {
return i-j;
}
}
diff --git a/source/com/c2kernel/utils/KeyValuePair.java b/source/com/c2kernel/utils/KeyValuePair.java index 149797e..5523eb6 100755..100644 --- a/source/com/c2kernel/utils/KeyValuePair.java +++ b/source/com/c2kernel/utils/KeyValuePair.java @@ -13,11 +13,11 @@ public class KeyValuePair mKey = key;
mValue = value;
}
-
+
public String getKey() {
return mKey;
}
-
+
public void setKey(String key) {
mKey = key;
}
@@ -25,22 +25,22 @@ public class KeyValuePair public Object getValue() {
return mValue;
}
-
+
public void setValue(Object value) {
mValue = value;
}
-
+
public String getStringValue() {
if (mValue instanceof String)
return (String)mValue;
else
return null;
}
-
+
public void setStringValue(String value) {
mValue = value;
}
-
+
public Integer getIntegerValue() {
if (mValue instanceof Integer)
return (Integer)mValue;
@@ -58,20 +58,20 @@ public class KeyValuePair else
return null;
}
-
+
public void setBooleanValue(Boolean value) {
mValue = value;
- }
-
+ }
+
public Float getFloatValue() {
if (mValue instanceof Float)
return (Float)mValue;
else
return null;
}
-
+
public void setFloatValue(Float value) {
mValue = value;
- }
-
+ }
+
}
diff --git a/source/com/c2kernel/utils/Language.java b/source/com/c2kernel/utils/Language.java index ee74f96..0b71d16 100644 --- a/source/com/c2kernel/utils/Language.java +++ b/source/com/c2kernel/utils/Language.java @@ -11,11 +11,11 @@ import java.util.Hashtable; public class Language
{
- public static Hashtable mTableOfTranslation = new Hashtable();
+ public static Hashtable<?, ?> mTableOfTranslation = new Hashtable<Object, Object>();
public static Hashtable<String, String> mTableOfUntranslated = new Hashtable<String, String>();
public static boolean isTranlated=false;
- private Hashtable tableOfTranslation = new Hashtable();
-
+ private Hashtable<?, ?> tableOfTranslation = new Hashtable<Object, Object>();
+
public static String translate(String english)
{
if (!isTranlated) return english;
@@ -23,13 +23,13 @@ public class Language if (rep != null && !rep.equals("")) {
String translated = (String) mTableOfTranslation.get(english);
if (translated != null) return translated;
- else
+ else
{
mTableOfUntranslated.put(english,"");
try
{
String s = "";
- Enumeration e = mTableOfUntranslated.keys();
+ Enumeration<String> e = mTableOfUntranslated.keys();
while (e.hasMoreElements()) s =s+"\n"+e.nextElement();
FileStringUtility.string2File("untranslated.txt",s);
}
@@ -42,11 +42,11 @@ public class Language }
return rep;
}
-
+
public Language(String filePath)
- {
+ {
String languageFile = filePath;
- if (languageFile == null || languageFile.length() == 0)
+ if (languageFile == null || languageFile.length() == 0)
// no language file defined for this process
return;
tableOfTranslation = FileStringUtility.loadLanguageFile(languageFile);
@@ -56,7 +56,7 @@ public class Language String rep = english;
if (rep != null && !rep.equals("")) {
String translated = (String) tableOfTranslation.get(english);
- if (translated != null)
+ if (translated != null)
rep = translated;
}
return rep;
diff --git a/source/com/c2kernel/utils/LocalObjectLoader.java b/source/com/c2kernel/utils/LocalObjectLoader.java index 620b030..8a73e3c 100755..100644 --- a/source/com/c2kernel/utils/LocalObjectLoader.java +++ b/source/com/c2kernel/utils/LocalObjectLoader.java @@ -14,8 +14,8 @@ import com.c2kernel.process.Gateway; public class LocalObjectLoader {
private static ActDefCache actCache = new ActDefCache();
- static public ItemProxy loadLocalObjectDef(String root, String name)
- throws ObjectNotFoundException
+ static public ItemProxy loadLocalObjectDef(String root, String name)
+ throws ObjectNotFoundException
{
DomainPath defRoot = new DomainPath(root);
DomainPath defPath = (DomainPath)defRoot.find(name);
@@ -32,7 +32,7 @@ public class LocalObjectLoader { Logger.error(ex);
throw new ObjectNotFoundException("Error loading script " + scriptName + " version " + scriptVersion, "");
}
-
+
}
static public Schema getSchema(String schemaName, int schemaVersion) throws ObjectNotFoundException {
@@ -40,14 +40,14 @@ public class LocalObjectLoader { Schema thisSchema = new Schema();
thisSchema.docType = schemaName;
thisSchema.docVersion = schemaVersion;
-
+
// don't bother if this is the Schema schema - for bootstrap esp.
if (schemaName.equals("Schema") && schemaVersion == 0) {
thisSchema.breakApart = false;
thisSchema.schema="";
return thisSchema;
}
-
+
ItemProxy schema = loadLocalObjectDef("/desc/OutcomeDesc/", schemaName);
Viewpoint schemaView = (Viewpoint)schema.getObject(ClusterStorage.VIEWPOINT + "/Schema/" + schemaVersion);
try {
@@ -58,18 +58,18 @@ public class LocalObjectLoader { }
String breakApart = schema.getProperty("BreakApart");
thisSchema.breakApart = breakApart.equals("1");
-
+
return thisSchema;
}
/**
* Retrieves a named version of activity def from the database
- *
+ *
* @param actName - activity name
* @param version - named version (String)
* @return ActivityDef
* @throws ObjectNotFoundException - When activity or version does not exist
- */
+ */
static public ActivityDef getActDef(String actName, String actVersion) throws ObjectNotFoundException, InvalidDataException {
Logger.msg(5, "Loading activity def "+actName+" v"+actVersion);
return actCache.get(actName, actVersion);
diff --git a/source/com/c2kernel/utils/Logger.java b/source/com/c2kernel/utils/Logger.java index 2d4d975..45edbee 100644 --- a/source/com/c2kernel/utils/Logger.java +++ b/source/com/c2kernel/utils/Logger.java @@ -32,9 +32,9 @@ public class Logger static private void printMessage(String message, int msgLogLevel)
{
synchronized(logStreams) {
- for (Iterator iter = logStreams.keySet().iterator(); iter.hasNext();) {
- PrintStream element = (PrintStream)iter.next();
- int logLevel = ((Integer)logStreams.get(element)).intValue();
+ for (Iterator<PrintStream> iter = logStreams.keySet().iterator(); iter.hasNext();) {
+ PrintStream element = iter.next();
+ int logLevel = logStreams.get(element).intValue();
if (logLevel < msgLogLevel || (logLevel > 9 && logLevel - 10 < msgLogLevel))
continue;
if (logLevel > 9)
@@ -51,7 +51,7 @@ public class Logger }
}
}
-
+
static private void printMessage(Throwable ex) {
StringWriter msgString = new StringWriter();
PrintWriter msg = new PrintWriter(msgString);
@@ -59,7 +59,7 @@ public class Logger ex.printStackTrace(msg);
printMessage(msgString.toString(), 0);
}
-
+
static public boolean doLog(int logLevel)
{
return mHighestLogLevel >= logLevel;
@@ -67,7 +67,7 @@ public class Logger /**
* Use this only for temporary messages while developing/debugging When the code is stable, change calls to debug to
* message/warning/error with an appropriate log level This makes it easier to manage debug calls in the source.
- *
+ *
* @param msg -
* the string to write to the console, or log file if specified in cmd line
*/
@@ -81,7 +81,7 @@ public class Logger }
/**
* Use Logger.message to report information that will be useful for debugging a release
- *
+ *
* @param level -
* log level of this message. If the current log level has been on the cmd line to be less that this number, the log message
* will not be displayed
@@ -143,23 +143,22 @@ public class Logger if (logLevel > 9) logLevel-=10;
if (logLevel > mHighestLogLevel) mHighestLogLevel = logLevel;
}
-
+
}
/**
* @param console
*/
public static void removeLogStream(PrintStream console) {
synchronized(logStreams) {
- Integer logIntObj = (Integer)logStreams.get(console);
- if (logIntObj == null) return; // not registered
+ Integer logIntObj = logStreams.get(console);
+ if (logIntObj == null) return; // not registered
int logLevel = (logIntObj).intValue();
logStreams.remove(console);
-
+
// recalculate lowest log level
if (logLevel == mHighestLogLevel || (logLevel > 9 && logLevel-10 == mHighestLogLevel)) {
mHighestLogLevel = -1;
- for (Iterator iter = logStreams.values().iterator(); iter.hasNext();) {
- Integer element = (Integer)iter.next();
+ for (Integer element : logStreams.values()) {
int thisLogLevel = element.intValue()>9?element.intValue()-10:element.intValue();
if (thisLogLevel > mHighestLogLevel || mHighestLogLevel == -1)
mHighestLogLevel = thisLogLevel;
@@ -167,7 +166,7 @@ public class Logger }
}
}
-
+
static public void initConsole(String id)
{
String portString = Gateway.getProperty(id+".Console.port");
@@ -177,19 +176,19 @@ public class Logger } catch (NumberFormatException ex) {
Logger.msg("No port defined for "+id+" console. Using any port.");
}
-
+
mConsole = new SimpleTCPIPServer(port, ScriptConsole.class, 5);
mConsole.startListening();
Gateway.setProperty(id+".Console.port", String.valueOf(mConsole.getPort()));
}
-
+
static public int getConsolePort() {
return mConsole.getPort();
}
-
+
static public void closeConsole()
{
if (mConsole != null)
- mConsole.stopListening();
+ mConsole.stopListening();
}
}
diff --git a/source/com/c2kernel/utils/Resource.java b/source/com/c2kernel/utils/Resource.java index 33100d8..361549d 100644 --- a/source/com/c2kernel/utils/Resource.java +++ b/source/com/c2kernel/utils/Resource.java @@ -62,7 +62,7 @@ public class Resource { static public URL getDomainResourceURL(String resName) throws MalformedURLException {
return new URL(domainBaseURL, resName);
}
-
+
private static URL getURLorResURL(String newURL) {
URL result;
try {
@@ -83,16 +83,16 @@ public class Resource { Logger.msg(8, "Resource::getTextResource() - Getting resource: " + resName);
if (txtCache.containsKey(resName)) {
- return (String)txtCache.get(resName);
+ return txtCache.get(resName);
}
try {
-
+
String newRes = null;
try {
newRes = FileStringUtility.url2String(getDomainResourceURL(resName));
} catch (Exception ex) { } // no domain base
-
+
if (newRes == null || newRes.length() == 0) { // not found in domain
newRes = FileStringUtility.url2String(getKernelResourceURL(resName));
}
@@ -104,7 +104,7 @@ public class Resource { }
/**
* Gets an image from the resource directories
- *
+ *
* @param resName - filename after resources/images
* @return
*/
@@ -116,13 +116,13 @@ public class Resource { return nullImg;
}
}
-
+
static public ImageIcon getImage(String resName) throws ObjectNotFoundException {
if (resName == null)
return nullImg;
if (imgCache.containsKey(resName)) {
- return (ImageIcon)imgCache.get(resName);
+ return imgCache.get(resName);
}
URL imgLocation = null;
@@ -134,7 +134,7 @@ public class Resource { newImg = new ImageIcon(imgLocation);
} catch (MalformedURLException e) { }
}
-
+
// try kernel resources next
if (newImg == null || newImg.getIconHeight() == -1) {
try {
@@ -142,12 +142,12 @@ public class Resource { newImg = new ImageIcon(imgLocation);
} catch (MalformedURLException e) { }
}
-
+
// else return null image
if (newImg == null || newImg.getIconHeight() == -1) {
throw new ObjectNotFoundException();
}
-
+
else imgCache.put(resName, newImg);
Logger.msg(7, "Loaded "+resName+" "+newImg.getIconWidth()+"x"+newImg.getIconHeight());
return newImg;
@@ -175,13 +175,13 @@ public class Resource { return "Domain application version not found";
}
}
-
+
static public String getKernelVersion() {
try {
return FileStringUtility.url2String(getKernelResourceURL("textFiles/version.txt"));
} catch (Exception ex) {
return "No version info found";
}
-
+
}
}
diff --git a/source/com/c2kernel/utils/SoftCache.java b/source/com/c2kernel/utils/SoftCache.java index 6680f30..9c95f2e 100644 --- a/source/com/c2kernel/utils/SoftCache.java +++ b/source/com/c2kernel/utils/SoftCache.java @@ -2,12 +2,16 @@ package com.c2kernel.utils; import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
-import java.util.*;
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Set;
/*******************************************************************************
* SoftReferences are reaped if no strong references are left and the vm is
* running out of memory. Most caches in the kernel use this.
- *
+ *
* $Revision: 1.5 $ $Date: 2004/10/29 13:29:09 $
******************************************************************************/
public class SoftCache<K, V> extends AbstractMap<K, V> {
@@ -43,7 +47,8 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { return result;
}
- public V put(K key, V value) {
+ @Override
+ public V put(K key, V value) {
processQueue();
if (minSize > 0) {
hardCache.addFirst(value);
@@ -54,33 +59,38 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { return value;
}
- public V remove(Object key) {
+ @Override
+ public V remove(Object key) {
processQueue();
return hash.remove(key).get();
}
- public void clear() {
+ @Override
+ public void clear() {
hardCache.clear();
while(queue.poll()!=null);
hash.clear();
}
- public int size() {
+ @Override
+ public int size() {
processQueue();
return hash.size();
}
-
- public Set<K> keySet() {
+
+ @Override
+ public Set<K> keySet() {
processQueue();
return hash.keySet();
}
- public Set<Map.Entry<K, V>> entrySet() {
+ @Override
+ public Set<Map.Entry<K, V>> entrySet() {
// Would have to create another Map to do this - too expensive
// Throwing runtime expensive is dangerous, but better than nulls
throw new UnsupportedOperationException();
- }
-
+ }
+
private static class SoftValue<V> extends SoftReference<V> {
private final Object key;
private SoftValue(Object key, V value, ReferenceQueue<V> q) {
@@ -88,15 +98,15 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { this.key = key;
}
}
-
+
/**
* Look for values that have been reaped, and remove their keys from the cache
*/
private void processQueue() {
- SoftValue sv;
- while ((sv = (SoftValue) queue.poll()) != null) {
+ SoftValue<?> sv;
+ while ((sv = (SoftValue<?>) queue.poll()) != null) {
hash.remove(sv.key);
}
}
-
+
}
diff --git a/source/com/c2kernel/utils/TransientCache.java b/source/com/c2kernel/utils/TransientCache.java index 8991b87..d58617e 100644 --- a/source/com/c2kernel/utils/TransientCache.java +++ b/source/com/c2kernel/utils/TransientCache.java @@ -1,9 +1,14 @@ package com.c2kernel.utils;
-import java.lang.ref.*;
-import java.util.*;
+import java.lang.ref.Reference;
+import java.util.AbstractMap;
+import java.util.AbstractSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
/**************************************************************************
- * TransientCache - Uses transient references to allow unused entries to be
+ * TransientCache - Uses transient references to allow unused entries to be
* reaped by the java garbage collector.
*
* $Revision: 1.1 $
@@ -13,10 +18,11 @@ import java.util.*; * All rights reserved.
**************************************************************************/
public abstract class TransientCache<K, V> extends AbstractMap<K, V> {
-
+
private Map<K, Reference<V>> map = new Hashtable<K, Reference<V>>();
-
- public synchronized Set<Entry<K, V>> entrySet() {
+
+ @Override
+ public synchronized Set<Entry<K, V>> entrySet() {
Map<K, V> newMap = new Hashtable<K,V>();
Iterator<Entry<K, Reference<V>>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
@@ -24,28 +30,30 @@ public abstract class TransientCache<K, V> extends AbstractMap<K, V> { Reference<V> ref = me.getValue();
V o = ref.get();
if (o == null) {
- // Delete cleared reference
+ // Delete cleared reference
iter.remove();
} else {
- // Copy out interior object
+ // Copy out interior object
newMap.put(me.getKey(), o);
}
}
- // Return set of interior objects
+ // Return set of interior objects
return newMap.entrySet();
}
-
- public synchronized V put(K key, V value) {
+
+ @Override
+ public synchronized V put(K key, V value) {
Reference<V> ref = makeReference(value);
- ref = (Reference<V>)map.put(key, ref);
+ ref = map.put(key, ref);
if (ref != null)
return (ref.get());
return null;
}
-
+
public abstract Reference<V> makeReference(Object value);
-
- public V remove(Object key) {
+
+ @Override
+ public V remove(Object key) {
Iterator<Entry<K, Reference<V>>> i = map.entrySet().iterator();
Entry<K, Reference<V>> correctEntry = null;
if (key == null) {
@@ -72,38 +80,46 @@ public abstract class TransientCache<K, V> extends AbstractMap<K, V> { /**
*
*/
- public void clear() {
+ @Override
+ public void clear() {
map.entrySet().clear();
}
-
+
private transient Set<K> keySet = null;
-
- public Set<K> keySet() {
+
+ @Override
+ public Set<K> keySet() {
if (keySet == null) {
keySet = new AbstractSet<K>() {
- public Iterator<K> iterator() {
+ @Override
+ public Iterator<K> iterator() {
return new Iterator<K>() {
private Iterator<Entry<K, Reference<V>>> i = map.entrySet().iterator();
- public boolean hasNext() {
+ @Override
+ public boolean hasNext() {
return i.hasNext();
}
- public K next() {
+ @Override
+ public K next() {
return i.next().getKey();
}
- public void remove() {
+ @Override
+ public void remove() {
i.remove();
}
};
}
- public int size() {
+ @Override
+ public int size() {
return TransientCache.this.size();
}
- public boolean contains(Object k) {
+ @Override
+ public boolean contains(Object k) {
return TransientCache.this.containsKey(k);
}
};
diff --git a/source/com/c2kernel/utils/XmlElementParser.java b/source/com/c2kernel/utils/XmlElementParser.java index 0f138d3..69cee31 100644 --- a/source/com/c2kernel/utils/XmlElementParser.java +++ b/source/com/c2kernel/utils/XmlElementParser.java @@ -24,7 +24,7 @@ public class XmlElementParser return parseOld(data, xpath);
}
}
-
+
public static String[] parseOld(String data, String path)
{
Vector<String> returnData = new Vector<String>();
@@ -41,13 +41,13 @@ public class XmlElementParser int i=taille;
while (pathTokens.hasMoreElements())
pathElements[--i] = pathTokens.nextToken();
-
+
if (Logger.doLog(6)) {
Logger.msg(6, "Path elements:");
- for (int x=0;x<pathElements.length;x++)
- Logger.debug(6, pathElements[x]);
+ for (String pathElement : pathElements)
+ Logger.debug(6, pathElement);
}
-
+
Logger.msg(6, "Looking for attribute "+pathElements[0]+" in "+pathElements[1]);
NodeList nl = doc.getElementsByTagName(pathElements[1]);
for (int j = 0; j < nl.getLength(); j++)
@@ -59,21 +59,21 @@ public class XmlElementParser for (int k=2;k<taille&&match;k++)
{
Logger.msg(6, "Checking parent "+pathElements[k]);
- child = child.getParentNode();
- if (!child.getNodeName().equals(pathElements[k]))
+ child = child.getParentNode();
+ if (!child.getNodeName().equals(pathElements[k]))
{
Logger.msg(6, "No match for "+child.getNodeName());
- match=false;
+ match=false;
}
else
Logger.msg(6, "Match");
}
if (match&&e.hasAttribute(pathElements[0])) {
- Logger.msg(6, "Matching Attribute "+pathElements[0]+"="+e.getAttribute(pathElements[0]));
+ Logger.msg(6, "Matching Attribute "+pathElements[0]+"="+e.getAttribute(pathElements[0]));
returnData.add(e.getAttribute(pathElements[0]));
}
}
-
+
Logger.msg(6, "Looking for element "+pathElements[0]);
nl = doc.getElementsByTagName(pathElements[0]);
for (int j = 0; j < nl.getLength(); j++)
@@ -85,16 +85,16 @@ public class XmlElementParser for (int k=1;k<taille&&match;k++)
{
Logger.msg(6, "Checking parent "+pathElements[k]);
- child = child.getParentNode();
- if (!child.getNodeName().equals(pathElements[k]))
+ child = child.getParentNode();
+ if (!child.getNodeName().equals(pathElements[k]))
{
Logger.msg(6, "No match for "+child.getNodeName());
- match=false;
+ match=false;
}
else
Logger.msg(6, "Match");
}
- if (match)
+ if (match)
{
String s =e.getFirstChild().getNodeValue();
Logger.msg(6, "Found Element "+pathElements[0]+"="+s);
@@ -109,7 +109,7 @@ public class XmlElementParser Logger.msg(3, returnData.size()+" values found for "+path);
returnArray=new String[returnData.size()];
for (int i=0;i<returnArray.length;i++)
- returnArray[i] = (String)returnData.get(i);
+ returnArray[i] = returnData.get(i);
return returnArray;
- }
+ }
}
diff --git a/source/com/c2kernel/utils/server/HTTPRequestHandler.java b/source/com/c2kernel/utils/server/HTTPRequestHandler.java index 78635e6..886ae9f 100644 --- a/source/com/c2kernel/utils/server/HTTPRequestHandler.java +++ b/source/com/c2kernel/utils/server/HTTPRequestHandler.java @@ -24,22 +24,26 @@ public class HTTPRequestHandler implements SocketHandler { protected final static String CRLF = "\r\n";
protected BufferedReader request;
protected Socket currentSocket = null;
-
+
public HTTPRequestHandler() { }
-
- public String getName() {
+
+ @Override
+ public String getName() {
return "HTTP Server";
}
- public boolean isBusy() {
+ @Override
+ public boolean isBusy() {
return (currentSocket!=null);
}
- public void setSocket(Socket newSocket) {
+ @Override
+ public void setSocket(Socket newSocket) {
currentSocket = newSocket;
}
-
- public void shutdown() {
+
+ @Override
+ public void shutdown() {
try {
if (currentSocket != null) currentSocket.close();
} catch (IOException e) {
@@ -47,11 +51,12 @@ public class HTTPRequestHandler implements SocketHandler { }
currentSocket = null;
}
-
- public void run() {
+
+ @Override
+ public void run() {
Thread.currentThread().setName("HTTP Request Handler");
try {
- request = new BufferedReader(new InputStreamReader(currentSocket.getInputStream()));
+ request = new BufferedReader(new InputStreamReader(currentSocket.getInputStream()));
// parse the request
boolean firstLine = true;
String headerLine;
@@ -70,7 +75,7 @@ public class HTTPRequestHandler implements SocketHandler { }
else { // headers
int split = headerLine.indexOf(": ");
- if (split >= 0)
+ if (split >= 0)
headers.put(
headerLine.substring(0, split),
headerLine.substring(split+2)
@@ -81,10 +86,10 @@ public class HTTPRequestHandler implements SocketHandler { String response = null;
try {
-
+
if (headers.containsKey("Content-length")) { // read POST data
StringBuffer postBuffer = new StringBuffer();
- int received = 0; int length = Integer.parseInt((String)headers.get("Content-length"));
+ int received = 0; int length = Integer.parseInt(headers.get("Content-length"));
try {
while (received < length) {
String postLine = request.readLine();
@@ -98,7 +103,7 @@ public class HTTPRequestHandler implements SocketHandler { }
}
// Got the params, generate response
-
+
returnMIME = "text/xml";
statusCode = "200 OK";
@@ -109,47 +114,47 @@ public class HTTPRequestHandler implements SocketHandler { System.out.println(new Date().toString()+" "+currentSocket.getInetAddress()+" "+method+" "+resource+" "+statusCode);
OutputStream output = currentSocket.getOutputStream();
-
+
statusCode = "HTTP/1.0 "+statusCode+CRLF;
output.write(statusCode.getBytes());
-
+
returnMIME = "Content-type: "+returnMIME+CRLF;
output.write(returnMIME.getBytes());
-
+
String contentLength = "Content-Length: "+response.length()+CRLF;
output.write(contentLength.getBytes());
-
+
// Possible: last mod?
// end of headers
output.write(CRLF.getBytes());
-
+
// write the content
output.write(response.getBytes());
request.close();
output.close();
currentSocket.close();
- } catch (IOException ex2) {
+ } catch (IOException ex2) {
Logger.error(ex2);
} // aborted connection probably
currentSocket = null;
}
-
+
/* This is a dummy method that doesn't support anything.
* Override it.
*/
-
+
public String processRequest() {
return error("501 Not Implemented", "The method "+method+" you have requested is not supported by this server.");
}
-
+
public String error(String code, String desc) {
statusCode = code;
returnMIME = "text/html";
- return ("<HTML>" +
+ return ("<HTML>" +
"<HEAD><TITLE>"+code+"</TITLE></HEAD>" +
"<BODY><h1>"+code+"</h1>" +
"<p>"+desc+
"<hr><p><small>Cristal Item HTTP server</small></BODY></HTML>");
}
}
-
+
diff --git a/source/com/c2kernel/utils/server/SimpleTCPIPServer.java b/source/com/c2kernel/utils/server/SimpleTCPIPServer.java index 8db9257..930fcee 100644 --- a/source/com/c2kernel/utils/server/SimpleTCPIPServer.java +++ b/source/com/c2kernel/utils/server/SimpleTCPIPServer.java @@ -1,11 +1,9 @@ package com.c2kernel.utils.server;
-import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
@@ -17,13 +15,13 @@ public class SimpleTCPIPServer implements Runnable int port = 0;
int maxConn = 10;
Thread listener = null;
- Class handlerClass = null;
+ Class<?> handlerClass = null;
ServerSocket serverSocket = null;
boolean keepListening = true;
ArrayList<SocketHandler> currentHandlers = new ArrayList<SocketHandler>();
static short noServers = 0;
- public SimpleTCPIPServer(int port, Class handlerClass, int maxConnections)
+ public SimpleTCPIPServer(int port, Class<?> handlerClass, int maxConnections)
{
this.port = port;
this.handlerClass = handlerClass;
@@ -35,26 +33,25 @@ public class SimpleTCPIPServer implements Runnable {
if(listener != null) return;
keepListening = true;
-
+
listener = new Thread(this);
listener.start();
}
-
+
public void stopListening()
{
Logger.msg("SimpleTCPIPServer: Closing server for " + handlerClass.getName() +" on port "+ port);
keepListening = false;
- for (Iterator iter = currentHandlers.iterator(); iter.hasNext();) {
- SocketHandler thisHandler = (SocketHandler)iter.next();
+ for (SocketHandler thisHandler : currentHandlers) {
thisHandler.shutdown();
}
}
- public void run()
+ @Override
+ public void run()
{
Thread.currentThread().setName("TCP/IP Server for "+handlerClass.getName());
Socket connectionSocket = null;
- InputStream inputStream = null;
try {
serverSocket = new ServerSocket(port);
@@ -64,11 +61,11 @@ public class SimpleTCPIPServer implements Runnable serverSocket.setSoTimeout(500);
SocketHandler freeHandler = null;
while(keepListening) {
- if (freeHandler == null || freeHandler.isBusy()) {
- ListIterator i = currentHandlers.listIterator();
+ if (freeHandler == null || freeHandler.isBusy()) {
+ ListIterator<SocketHandler> i = currentHandlers.listIterator();
try {
do {
- freeHandler = (SocketHandler)i.next();
+ freeHandler = i.next();
} while (freeHandler.isBusy());
} catch (NoSuchElementException e) {
// create new one
@@ -105,7 +102,7 @@ public class SimpleTCPIPServer implements Runnable listener = null;
Logger.msg("SimpleTCPIPServer - Servers still running: "+--noServers);
}
-
+
public int getPort() {
return port;
}
diff --git a/source/com/c2kernel/utils/server/SocketHandler.java b/source/com/c2kernel/utils/server/SocketHandler.java index 455a9bd..8d37714 100755..100644 --- a/source/com/c2kernel/utils/server/SocketHandler.java +++ b/source/com/c2kernel/utils/server/SocketHandler.java @@ -3,13 +3,13 @@ package com.c2kernel.utils.server; import java.net.Socket;
public interface SocketHandler extends Runnable {
-
+
public String getName();
-
+
public boolean isBusy();
-
+
public void setSocket(Socket newSocket);
-
+
public void shutdown();
}
-
+
diff --git a/source/com/c2kernel/utils/server/UDPListener.java b/source/com/c2kernel/utils/server/UDPListener.java index 1f02361..3b1fc9d 100755..100644 --- a/source/com/c2kernel/utils/server/UDPListener.java +++ b/source/com/c2kernel/utils/server/UDPListener.java @@ -18,15 +18,16 @@ import com.c2kernel.utils.Logger; **************************************************************************/
public abstract class UDPListener extends Thread {
-
+
private boolean active = true;
protected DatagramSocket socket;
-
+
public UDPListener() {
super();
}
-
- public void run() {
+
+ @Override
+ public void run() {
Thread.currentThread().setName("UDP Server");
byte[] buffer = new byte[255];
DatagramPacket newPacket = new DatagramPacket(buffer, buffer.length);
@@ -44,10 +45,10 @@ public abstract class UDPListener extends Thread { }
socket.close();
}
-
+
protected abstract void processPacket(DatagramPacket packet) throws InvalidDataException;
-
+
public void shutdown() {
- active = false;
+ active = false;
}
}
|
