/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.env;
import java.io.InputStream;
import org.jbpm.client.CommandService;
import org.jbpm.env.impl.DefaultEnvironmentFactory;
import org.jbpm.env.xml.EnvironmentParser;
import org.jbpm.xml.Parse;
import org.jbpm.xml.Parser;
import org.xml.sax.InputSource;
/**
* factory for {@link Environment}s.
*
* <p>Default implementation is
* {@link DefaultEnvironmentFactory}. EnvironmentFactory is thread safe, you
* should use one environment factory for all your threads.
* </p>
*
* <p>Easiest way to obtain an EnvironmentFactory is with
* <ul>
* <li>{@link #parseResource(String)}</li>
* <li>{@link #parseInputStream(InputStream)}</li>
* <li>{@link #parseInputSource(InputSource)}</li>
* <li>{@link #parseXmlString(String)}</li>
* </ul>
* </p>
*
* <p>For the default parser's XML schema, see {@link EnvironmentParser}.</p>
*
* @author Tom Baeyens
*/
public abstract class EnvironmentFactory implements Context {
protected static Parser parser = null;
/** returns an {@link EnvironmentParser} unless another parser is explicitely specified
* with {@link #setParser(Parser)}. This method is used to get a parser for methods
* {@link #parseResource(String)}, {@link #parseInputStream(InputStream)},
* {@link #parseInputSource(InputSource)} and {@link #parseXmlString(String)}. */
public static synchronized Parser getParser() {
if (parser==null) {
parser = new EnvironmentParser();
}
return parser;
}
/** overwrites {@link EnvironmentParser the default parser} with a custom parser
* that will be used in methods {@link #parseResource(String)},
* {@link #parseInputStream(InputStream)}, {@link #parseInputSource(InputSource)}
* and {@link #parseXmlString(String)}. */
public static synchronized void setParser(Parser parser) {
EnvironmentFactory.parser = parser;
}
/** parses the given resource with {@link #getParser() the current parser}. */
public static EnvironmentFactory parseResource(String resource) {
Parse parse = new Parse();
EnvironmentFactory environmentFactory = (EnvironmentFactory) getParser().parseResource(resource, null, parse);
parse.checkProblems("EnvironmentFactory");
return environmentFactory;
}
/** parses the given xmlString with {@link #getParser() the current parser}. */
public static EnvironmentFactory parseXmlString(String xmlString) {
Parse parse = new Parse();
EnvironmentFactory environmentFactory = (EnvironmentFactory) getParser().parseXmlString(xmlString, parse);
parse.checkProblems("EnvironmentFactory");
return environmentFactory;
}
/** parses the given inputSource with {@link #getParser() the current parser}. */
public static EnvironmentFactory parseInputSource(InputSource inputSource) {
Parse parse = new Parse();
EnvironmentFactory environmentFactory = (EnvironmentFactory) getParser().parseInputSource(inputSource, parse);
parse.checkProblems("EnvironmentFactory");
return environmentFactory;
}
/** parses the given inputStream with {@link #getParser() the current parser}. */
public static EnvironmentFactory parseInputStream(InputStream inputStream) {
Parse parse = new Parse();
EnvironmentFactory environmentFactory = (EnvironmentFactory) getParser().parseInputStream(inputStream, parse);
parse.checkProblems("EnvironmentFactory");
return environmentFactory;
}
/**
* open a new Environment. The client is responsible for
* closing the environment with {@link Environment#close()}.
*/
public abstract Environment openEnvironment();
/**
* closes this environment factory and cleans any allocated
* resources.
*/
public abstract void close();
/** after opening of a new environment succeeded, the environment
* must be pushed in the stack of current environments.
*
* @see Environment#pop() */
protected static synchronized void push(Environment environment) {
Environment current = Environment.currentEnvironment.get();
if (current!=null) {
Environment.getStack().push(current);
}
Environment.currentEnvironment.set(environment);
}
}