You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/05/24 03:03:44 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata RepositoryPersistor.java
arminw 2003/05/23 18:03:44
Modified: src/java/org/apache/ojb/broker/metadata
RepositoryPersistor.java
Log:
add new methods
Revision Changes Path
1.19 +78 -36 db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java
Index: RepositoryPersistor.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- RepositoryPersistor.java 23 Apr 2003 08:10:43 -0000 1.18
+++ RepositoryPersistor.java 24 May 2003 01:03:44 -0000 1.19
@@ -62,7 +62,11 @@
import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
import org.apache.ojb.broker.util.logging.Logger;
import org.apache.ojb.broker.util.logging.LoggerFactory;
-import org.xml.sax.*;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
@@ -71,8 +75,9 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.PrintWriter;
import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
@@ -88,11 +93,13 @@
*/
public class RepositoryPersistor implements Configurable
{
+ // TODO: Refactoring of the metadata reading/handling?
+
private static Logger log = LoggerFactory.getLogger(RepositoryPersistor.class);
private static final String SER_FILE_SUFFIX = "serialized";
- private static final String SERIALIZED_REPOSITORY_PATH = "serializedRepositoryPath";
-
+ private static final String SERIALIZED_REPOSITORY_PATH = "serializedRepositoryPath";
+
private boolean useSerializedRepository = false;
public RepositoryPersistor()
@@ -106,16 +113,17 @@
}
/**
- * Write the {@link DescriptorRepository} to the given file name.
+ * Write the {@link DescriptorRepository} to the given output object.
*/
public void writeToFile(DescriptorRepository repository, ConnectionRepository conRepository, OutputStream out)
{
RepositoryTags tags = RepositoryTags.getInstance();
try
{
- if(log.isDebugEnabled()) log.debug("## Write repository file ##"+
- repository.toXML()+
- "## End of repository file ##");
+ if (log.isDebugEnabled())
+ log.debug("## Write repository file ##" +
+ repository.toXML() +
+ "## End of repository file ##");
String eol = SystemUtils.LINE_SEPARATOR;
@@ -123,7 +131,7 @@
// 1. write XML header
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + eol);
- buf.append("<!DOCTYPE descriptor-repository SYSTEM \"repository.dtd\" >"+ eol + eol);
+ buf.append("<!DOCTYPE descriptor-repository SYSTEM \"repository.dtd\" >" + eol + eol);
// strReturn += "<!DOCTYPE descriptor-repository SYSTEM \"repository.dtd\" [" + eol;
// strReturn += "<!ENTITY database-metadata SYSTEM \""+ConnectionRepository.DATABASE_METADATA_FILENAME+"\">" + eol;
// strReturn += "<!ENTITY user SYSTEM \"repository_user.xml\">" + eol;
@@ -137,8 +145,8 @@
buf.append(" " + tags.getAttribute(RepositoryElements.ISOLATION_LEVEL, repository.getIsolationLevelAsString()) + eol);
buf.append(">" + eol);
- buf.append(eol+eol+conRepository.toXML()+eol+eol);
- buf.append(repository.toXML());
+ if(conRepository != null) buf.append(eol + eol + conRepository.toXML() + eol + eol);
+ if(repository != null) buf.append(repository.toXML());
buf.append(tags.getClosingTagById(RepositoryElements.MAPPING_REPOSITORY));
@@ -171,19 +179,19 @@
if (useSerializedRepository)
// use serialized repository
{
- // build File object pointing to serialized repository location
- Configuration config = OjbConfigurator.getInstance().getConfigurationFor(null);
- String pathPrefix = config.getString(SERIALIZED_REPOSITORY_PATH, ".");
- File serFile = new File(pathPrefix + File.separator + filename + "." + SER_FILE_SUFFIX);
+ // build File object pointing to serialized repository location
+ Configuration config = OjbConfigurator.getInstance().getConfigurationFor(null);
+ String pathPrefix = config.getString(SERIALIZED_REPOSITORY_PATH, ".");
+ File serFile = new File(pathPrefix + File.separator + filename + "." + SER_FILE_SUFFIX);
- if(serFile.exists() && serFile.length() > 0)
+ if (serFile.exists() && serFile.length() > 0)
// if file exists load serialized version of repository
{
try
{
long duration = System.currentTimeMillis();
result = deserialize(serFile);
- log.info("Read serialized repository in "+(System.currentTimeMillis()-duration)+" ms");
+ log.info("Read serialized repository in " + (System.currentTimeMillis() - duration) + " ms");
}
catch (Exception e)
{
@@ -196,7 +204,7 @@
{
long duration = System.currentTimeMillis();
result = (DescriptorRepository) buildRepository(filename, DescriptorRepository.class);
- log.info("Read repository from file took "+(System.currentTimeMillis()-duration)+" ms");
+ log.info("Read repository from file took " + (System.currentTimeMillis() - duration) + " ms");
serialize(result, serFile);
}
}
@@ -205,11 +213,21 @@
{
long duration = System.currentTimeMillis();
result = (DescriptorRepository) buildRepository(filename, DescriptorRepository.class);
- log.info("Read repository from file took "+(System.currentTimeMillis()-duration)+" ms");
+ log.info("Read class descriptors took " + (System.currentTimeMillis() - duration) + " ms");
}
return result;
}
+ public DescriptorRepository readDescriptorRepository(InputStream inst)
+ throws MalformedURLException, ParserConfigurationException, SAXException, IOException
+ {
+ long duration = System.currentTimeMillis();
+ InputSource inSource = new InputSource(inst);
+ DescriptorRepository result = (DescriptorRepository) readMetadataFromXML(inSource, DescriptorRepository.class);
+ log.info("Read class descriptors took " + (System.currentTimeMillis() - duration) + " ms");
+ return result;
+ }
+
/**
* Read the repository configuration file and extract connection handling information.
*/
@@ -218,7 +236,20 @@
{
long duration = System.currentTimeMillis();
ConnectionRepository result = (ConnectionRepository) buildRepository(filename, ConnectionRepository.class);
- log.info("Read connection repository from file took "+(System.currentTimeMillis()-duration)+" ms");
+ log.info("Read connection repository took " + (System.currentTimeMillis() - duration) + " ms");
+ return result;
+ }
+
+ /**
+ * Read the repository configuration file and extract connection handling information.
+ */
+ public ConnectionRepository readConnectionRepository(InputStream inst)
+ throws MalformedURLException, ParserConfigurationException, SAXException, IOException
+ {
+ long duration = System.currentTimeMillis();
+ InputSource inSource = new InputSource(inst);
+ ConnectionRepository result = (ConnectionRepository) readMetadataFromXML(inSource, ConnectionRepository.class);
+ log.info("Read connection repository took " + (System.currentTimeMillis() - duration) + " ms");
return result;
}
@@ -259,7 +290,17 @@
throws MalformedURLException, ParserConfigurationException, SAXException, IOException
{
URL url = buildURL(repositoryFileName);
+ InputSource source = new InputSource(url.openStream());
+ return readMetadataFromXML(source, targetRepository);
+ }
+ /**
+ * Read metadata by populating an instance of the target class
+ * using SAXParser.
+ */
+ private Object readMetadataFromXML(InputSource source, Class target)
+ throws MalformedURLException, ParserConfigurationException, SAXException, IOException
+ {
// get a xml reader instance:
SAXParser p = SAXParserFactory.newInstance().newSAXParser();
XMLReader reader = p.getXMLReader();
@@ -272,20 +313,20 @@
* called on crimson parser, the parser does not find any relative path
* TODO better solution
*/
- try
- {
- if(reader.getClass().getName().indexOf("crimson") != -1)
+ try
+ {
+ if (reader.getClass().getName().indexOf("crimson") != -1)
{
reader.setFeature("http://xml.org/sax/features/validation", true);
}
- }
- catch (SAXNotSupportedException snse)
- {
- // ignore
- }
- Object result = null;
+ }
+ catch (SAXNotSupportedException snse)
+ {
+ // ignore
+ }
- if(DescriptorRepository.class.equals(targetRepository))
+ Object result = null;
+ if (DescriptorRepository.class.equals(target))
{
// create an empty repository:
DescriptorRepository repository = new DescriptorRepository();
@@ -293,10 +334,10 @@
ContentHandler handler = new RepositoryXmlHandler(repository);
// tell parser to use our handler:
reader.setContentHandler(handler);
- reader.parse(url.toString());
+ reader.parse(source);
result = repository;
}
- else if(ConnectionRepository.class.equals(targetRepository))
+ else if (ConnectionRepository.class.equals(target))
{
// create an empty repository:
ConnectionRepository repository = new ConnectionRepository();
@@ -304,12 +345,13 @@
ContentHandler handler = new ConnectionDescriptorXmlHandler(repository);
// tell parser to use our handler:
reader.setContentHandler(handler);
- reader.parse(url.toString());
+ reader.parse(source);
//LoggerFactory.getBootLogger().info("loading XML took " + (stop - start) + " msecs");
result = repository;
}
- else throw new MetadataException("Could not build a repository instance for '"+targetRepository+
- "', try to read data using "+repositoryFileName);
+ else
+ throw new MetadataException("Could not build a repository instance for '" + target +
+ "', using source " + source);
return result;
}