001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.util;
024:
025: import java.lang.reflect.InvocationTargetException;
026:
027: import biz.hammurapi.config.Component;
028: import biz.hammurapi.config.ConfigurationException;
029: import biz.hammurapi.config.Context;
030: import biz.hammurapi.config.Wrapper;
031: import biz.hammurapi.sql.SQLProcessor;
032:
033: /**
034: * Helper class to instantiate data access engines generated by SQLC.
035: * @author Pavel Vlasov
036: */
037: public class EngineWrapper implements Wrapper, Component {
038:
039: private String engineClassName;
040: private String processorLocation = "/sql-processor";
041: private Object master;
042: private Object owner;
043: private boolean started;
044:
045: public EngineWrapper(String config) {
046: int idx = config.indexOf(",");
047: if (idx == -1) {
048: this .engineClassName = config;
049: } else {
050: this .engineClassName = config.substring(0, idx);
051: this .processorLocation = config.substring(idx + 1);
052: }
053:
054: }
055:
056: public Object getMaster() {
057: if (!started) {
058: throw new IllegalStateException("Not started");
059: }
060: return master;
061: }
062:
063: public void start() throws ConfigurationException {
064: SQLProcessor processor = (SQLProcessor) ((Context) owner)
065: .get(processorLocation);
066: try {
067: master = Class.forName(engineClassName).getConstructor(
068: new Class[] { SQLProcessor.class }).newInstance(
069: new Object[] { processor });
070: } catch (InstantiationException e) {
071: throw new ConfigurationException(
072: "Could not create engine instance "
073: + engineClassName, e);
074: } catch (IllegalAccessException e) {
075: throw new ConfigurationException(
076: "Could not create engine instance "
077: + engineClassName, e);
078: } catch (InvocationTargetException e) {
079: throw new ConfigurationException(
080: "Could not create engine instance "
081: + engineClassName, e);
082: } catch (NoSuchMethodException e) {
083: throw new ConfigurationException(
084: "Could not create engine instance "
085: + engineClassName, e);
086: } catch (ClassNotFoundException e) {
087: throw new ConfigurationException(
088: "Could not create engine instance "
089: + engineClassName, e);
090: }
091: started = true;
092: }
093:
094: public void stop() throws ConfigurationException {
095: master = null;
096: started = false;
097: }
098:
099: public void setOwner(Object owner) {
100: this.owner = owner;
101: }
102:
103: }
|