You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jm...@apache.org on 2001/10/21 02:18:40 UTC
cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/intake TurbineIntakeService.java
jmcnally 01/10/20 17:18:40
Modified: src/java/org/apache/turbine/services/intake
TurbineIntakeService.java
Log:
removed use of Introspector due to bugs. The code is actually cleaner without
it. Also added Jon's patches to properly close streams.
Revision Changes Path
1.3 +115 -61 jakarta-turbine-2/src/java/org/apache/turbine/services/intake/TurbineIntakeService.java
Index: TurbineIntakeService.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/intake/TurbineIntakeService.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TurbineIntakeService.java 2001/08/18 01:56:24 1.2
+++ TurbineIntakeService.java 2001/10/21 00:18:39 1.3
@@ -91,7 +91,7 @@
* on an XML specification.
*
* @author <a href="mailto:jmcnally@collab.net">John McNally</a>
- * @version $Id: TurbineIntakeService.java,v 1.2 2001/08/18 01:56:24 mpoeschl Exp $
+ * @version $Id: TurbineIntakeService.java,v 1.3 2001/10/21 00:18:39 jmcnally Exp $
*/
public class TurbineIntakeService
extends TurbineBaseService
@@ -164,21 +164,29 @@
if ( serialAppData.exists()
&& serialAppData.lastModified() > xmlFile.lastModified() )
{
- InputStream in = new FileInputStream(serialAppData);
- ObjectInputStream p = new ObjectInputStream(in);
- appData = (AppData)p.readObject();
- in.close();
+ InputStream in = null;
+ try
+ {
+ in = new FileInputStream(serialAppData);
+ ObjectInputStream p = new ObjectInputStream(in);
+ appData = (AppData)p.readObject();
+ }
+ catch (Exception e)
+ {
+ // We got a corrupt file for some reason
+ writeAppData(xmlPath, appDataPath, serialAppData);
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+ }
}
else
{
- XmlToAppData xmlApp = new XmlToAppData();
- appData = xmlApp.parseFile(xmlPath);
-
- OutputStream out = new FileOutputStream(serialAppData);
- ObjectOutputStream p = new ObjectOutputStream(out);
- p.writeObject(appData);
- p.flush();
- out.close();
+ writeAppData(xmlPath, appDataPath, serialAppData);
}
groupNames = new String[appData.getGroups().size()];
@@ -219,51 +227,52 @@
"TurbineIntakeService failed to initialize", e);
}
}
+
- private Method initializeBeanProp(String className, String propName,
- int getOrSet)
+ /**
+ * This method writes the appData file into Objects and stores
+ * the information into this classes appData property
+ */
+ private void writeAppData(String xmlPath, String appDataPath, File serialAppData)
throws Exception
{
- // getter or setter method
- Method method = null;
- if ( className != null && propName != null )
- {
- // Uses bean introspection to set writable properties
- // of bean from the parameters, where a
- // (case-insensitive) name match between the bean
- // property and the parameter is looked for.
- PropertyDescriptor[] beanProps = Introspector
- .getBeanInfo(Class.forName(className))
- .getPropertyDescriptors();
-
- boolean noMatch = true;
- for (int j=beanProps.length-1; j>=0; j--)
+ XmlToAppData xmlApp = new XmlToAppData();
+ appData = xmlApp.parseFile(xmlPath);
+ OutputStream out = null;
+ InputStream in = null;
+ try
+ {
+ // write the appData file out
+ out = new FileOutputStream(serialAppData);
+ ObjectOutputStream p = new ObjectOutputStream(out);
+ p.writeObject(appData);
+ p.flush();
+
+ // read the file back in. for some reason on OSX 10.1
+ // this is necessary.
+ in = new FileInputStream(serialAppData);
+ ObjectInputStream pin = new ObjectInputStream(in);
+ appData = (AppData)pin.readObject();
+ }
+ catch (Exception e)
+ {
+ Log.info(
+ "Intake initialization could not be serialized " +
+ "because writing to " + appDataPath + " was not " +
+ "allowed. This will require that the xml file be " +
+ "parsed when restarting the application.");
+ }
+ finally
+ {
+ if (out != null)
{
- if (propName.equalsIgnoreCase(beanProps[j].getName()))
- {
- switch ( getOrSet )
- {
- case GETTER:
- method = beanProps[j].getReadMethod();
- ((HashMap)getterMap.get(className))
- .put(propName, method);
- break;
- case SETTER:
- method = beanProps[j].getWriteMethod();
- ((HashMap)setterMap.get(className))
- .put(propName, method);
- }
- noMatch = false;
- break;
- }
+ out.close();
}
- if ( noMatch )
+ if (in != null)
{
- Log.error("Property, " + propName + " for class, " +
- className + " could not be found.");
+ in.close();
}
}
- return method;
}
/**
@@ -551,15 +560,39 @@
public Method getFieldSetter(String className, String propName)
{
Map settersForClassName = (Map)setterMap.get(className);
- Method method = (Method)settersForClassName.get(propName);
+ Method setter = (Method)settersForClassName.get(propName);
- if ( method == null )
+ if ( setter == null )
{
+ PropertyDescriptor pd = null;
synchronized(setterMap)
{
try
+ {
+ pd = new PropertyDescriptor(propName,
+ Class.forName(className));
+ setter = pd.getWriteMethod();
+ ((Map)setterMap.get(className)).put(propName, setter);
+ if ( setter == null )
+ {
+ Log.error("Intake: setter for '" + propName
+ + "' in class '" + className
+ + "' could not be found.");
+ }
+ }
+ catch (Exception e)
+ {
+ Log.error(e);
+ }
+ }
+ // we have already completed the reflection on the getter, so
+ // save it so we do not have to repeat
+ synchronized(getterMap)
+ {
+ try
{
- method = initializeBeanProp(className, propName, SETTER);
+ Method getter = pd.getReadMethod();
+ ((Map)getterMap.get(className)).put(propName, getter);
}
catch (Exception e)
{
@@ -567,8 +600,7 @@
}
}
}
-
- return method;
+ return setter;
}
/**
@@ -581,15 +613,39 @@
public Method getFieldGetter(String className, String propName)
{
Map gettersForClassName = (Map)getterMap.get(className);
- Method method = (Method)gettersForClassName.get(propName);
+ Method getter = (Method)gettersForClassName.get(propName);
- if ( method == null )
+ if ( getter == null )
{
+ PropertyDescriptor pd = null;
synchronized(getterMap)
{
try
{
- method = initializeBeanProp(className, propName, GETTER);
+ pd = new PropertyDescriptor(propName,
+ Class.forName(className));
+ getter = pd.getReadMethod();
+ ((Map)getterMap.get(className)).put(propName, getter);
+ if ( getter == null )
+ {
+ Log.error("Intake: getter for '" + propName
+ + "' in class '" + className
+ + "' could not be found.");
+ }
+ }
+ catch (Exception e)
+ {
+ Log.error(e);
+ }
+ }
+ // we have already completed the reflection on the setter, so
+ // save it so we do not have to repeat
+ synchronized(setterMap)
+ {
+ try
+ {
+ Method setter = pd.getWriteMethod();
+ ((Map)setterMap.get(className)).put(propName, setter);
}
catch (Exception e)
{
@@ -597,8 +653,6 @@
}
}
}
-
- return method;
+ return getter;
}
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org