001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.ant;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.StringTokenizer;
027:
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.Task;
030: import org.hibernate.cfg.Configuration;
031: import org.hibernate.tool.hbm2ddl.SchemaExport;
032: import org.jbpm.JbpmConfiguration;
033: import org.jbpm.JbpmContext;
034: import org.jbpm.db.JbpmSchema;
035: import org.jbpm.persistence.db.DbPersistenceServiceFactory;
036: import org.jbpm.svc.Services;
037:
038: public class JbpmSchemaTask extends Task {
039:
040: String jbpmCfg = null;
041: String hibernateCfg = null;
042: String hibernateProperties = null;
043:
044: boolean quiet = false;
045: boolean text = false;
046: String output = null;
047: String delimiter = null;
048:
049: String actions = null;
050:
051: public void execute() throws BuildException {
052: if (actions == null) {
053: // default action is create
054: actions = "create";
055: }
056:
057: // we need a configuration
058: Configuration configuration = null;
059:
060: // if there is no jbpm nor hibernate configuration specified
061: if ((jbpmCfg == null) && (hibernateCfg == null)) {
062: // search for the default jbpm.cfg.xml
063: InputStream defaultJbpmCfgStream = getClass()
064: .getClassLoader().getResourceAsStream(
065: "jbpm.cfg.xml");
066: if (defaultJbpmCfgStream != null) {
067: jbpmCfg = "jbpm.cfg.xml";
068: try {
069: defaultJbpmCfgStream.close();
070: } catch (IOException e) {
071: e.printStackTrace();
072: }
073:
074: // if still not found, search for the default hibernate.cfg.xml
075: } else {
076: InputStream defaultHibernateCfgStream = getClass()
077: .getClassLoader().getResourceAsStream(
078: "hibernate.cfg.xml");
079: if (defaultHibernateCfgStream != null) {
080: hibernateCfg = "hibernate.cfg.xml";
081: try {
082: defaultHibernateCfgStream.close();
083: } catch (IOException e) {
084: e.printStackTrace();
085: }
086: }
087: }
088: }
089:
090: // first see if the jbpm cfg is specified cause that implies a hibernate configuration
091: if (jbpmCfg != null) {
092: log("using jbpm configuration " + jbpmCfg);
093: JbpmConfiguration jbpmConfiguration = AntHelper
094: .getJbpmConfiguration(jbpmCfg);
095: JbpmContext jbpmContext = jbpmConfiguration
096: .createJbpmContext();
097: try {
098: DbPersistenceServiceFactory dbPersistenceServiceFactory = (DbPersistenceServiceFactory) jbpmConfiguration
099: .getServiceFactory(Services.SERVICENAME_PERSISTENCE);
100: configuration = dbPersistenceServiceFactory
101: .getConfiguration();
102: } finally {
103: jbpmContext.close();
104: }
105:
106: // if there is no jbpm.cfg.xml specified, check if there is a hibernate.cfg.xml specified
107: } else if (hibernateCfg != null) {
108: log("using hibernate configuration " + hibernateCfg);
109: configuration = AntHelper.getConfiguration(hibernateCfg,
110: hibernateProperties);
111:
112: // no hibernate configuration specified
113: } else {
114: throw new BuildException(
115: "couldn't create schema. no jbpm nor hibernate configuration specified.");
116: }
117:
118: JbpmSchema jbpmSchema = new JbpmSchema(configuration);
119:
120: SchemaExport schemaExport = new SchemaExport(configuration);
121: if (output != null)
122: schemaExport.setOutputFile(output);
123: if (delimiter != null)
124: schemaExport.setDelimiter(delimiter);
125:
126: StringTokenizer tokenizer = new StringTokenizer(actions, ",");
127: while (tokenizer.hasMoreTokens()) {
128: String action = tokenizer.nextToken();
129:
130: if ("drop".equalsIgnoreCase(action)) {
131: schemaExport.drop(!quiet, !text);
132:
133: } else if ("create".equalsIgnoreCase(action)) {
134: schemaExport.create(!quiet, !text);
135:
136: } else if ("clean".equalsIgnoreCase(action)) {
137: jbpmSchema.cleanSchema();
138: }
139: }
140: }
141:
142: public void setActions(String actions) {
143: this .actions = actions;
144: }
145:
146: public void setJbpmCfg(String jbpmCfg) {
147: this .jbpmCfg = jbpmCfg;
148: }
149:
150: public void setHibernateCfg(String hibernateCfg) {
151: this .hibernateCfg = hibernateCfg;
152: }
153:
154: public void setHibernateProperties(String hibernateProperties) {
155: this .hibernateProperties = hibernateProperties;
156: }
157:
158: public void setDelimiter(String delimiter) {
159: this .delimiter = delimiter;
160: }
161:
162: public void setOutput(String output) {
163: this .output = output;
164: }
165:
166: public void setQuiet(boolean quiet) {
167: this .quiet = quiet;
168: }
169:
170: public void setText(boolean text) {
171: this.text = text;
172: }
173: }
|