View Javadoc

1   package org.apache.maven.hibernate.beans;
2   
3   /* ====================================================================
4    *   Copyright 2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.File;
21  import java.net.URL;
22  import java.net.URLClassLoader;
23  
24  import net.sf.hibernate.HibernateException;
25  import net.sf.hibernate.cfg.Configuration;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * 
32   * Base class for Beans which serve as Proxy To Hibernate API
33   * <br/>
34   * 
35   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
36   * @author <a href="maven@felipeal.net">Felipe Leme</a> 
37   * @version $Id: SchemaBeanBase.java 170200 2005-05-15 06:24:19Z brett $
38   */
39  public abstract class SchemaBeanBase extends CommonOperationsBean
40  {
41  
42     private String properties = null;
43     private String config = null;
44     private boolean quiet = false;
45     private boolean text = false;
46     
47     protected static final Log LOG = LogFactory.getLog(MappingsAggregatorBean.class);
48  
49  
50     
51     /**
52      * @return
53      */
54     public boolean getQuiet()
55     {
56        return quiet;
57     }
58  
59     /**
60      * @return
61      */
62     public boolean getText()
63     {
64        return text;
65     }
66     
67     /**
68      * @param b
69      */
70     public void setQuiet(boolean b)
71     {
72        quiet = b;
73     }
74  
75     /**
76      * @param b
77      */
78     public void setText(boolean b)
79     {
80        text = b;
81     }
82  
83     /**
84      * @return
85      */
86     public String getConfig()
87     {
88        return config;
89     }
90  
91     /**
92      * @return
93      */
94     public String getProperties()
95     {
96        return properties;
97     }
98  
99     /**
100     * @param string
101     */
102    public void setConfig(String string)
103    {
104       config = string;
105    }
106 
107    /**
108     * @param string
109     */
110    public void setProperties(String string)
111    {
112       properties = string;
113    }
114 
115    /**
116     * 
117     * Hibernate requires that 
118     * Java classes (beans) are accesible on the
119     * classpath. As they are not in plugin classpath
120     * we have to take care about. 
121     * To assure that we have them visible for plugin
122     * classloader
123     * we will make temporay change to context classloader
124     * which will be restored when method terminates.
125     * 
126     */
127    public void execute() throws Exception
128    {
129 
130       Thread currentThread = Thread.currentThread();
131       ClassLoader oldClassLoader = currentThread.getContextClassLoader();
132       try
133       {
134   	    File [] baseDirs = getBaseDirs ();
135   	    URL  [] urls = new URL [baseDirs.length];
136   	    for (int i = 0; i < urls.length; i++) { 
137   		    urls [i] = baseDirs [i].toURL ();
138   	    }
139   	
140         URLClassLoader newClassLoader =
141           new URLClassLoader(urls, getClass().getClassLoader());
142         currentThread.setContextClassLoader(newClassLoader);
143     
144         Configuration cfg = getConfiguration();        
145         executeSchema( cfg );
146 
147       }
148       finally
149       {
150          currentThread.setContextClassLoader(oldClassLoader);
151       }
152    }
153 
154    /**
155    * Method that does the real job
156    */
157   protected abstract void executeSchema(Configuration cfg) throws Exception;
158 
159   /**
160     * 
161     */
162    private Configuration getConfiguration() throws HibernateException
163    {
164       Configuration cfg = new Configuration();
165       if (getConfig() != null)
166       {      	
167       	 File f = new File(getConfig());
168       	 LOG.debug("Hibernate Configuration File: " + f.getAbsolutePath());
169          cfg.configure(f);
170       }
171 
172       String[] files = getFileNames ();
173       for (int i = 0; i < files.length; i++)
174       {
175          String filename = files[i];
176          if (filename.endsWith(".jar"))
177          {
178             cfg.addJar(filename);
179          }
180          else
181          {
182             cfg.addFile(filename);
183          }
184       }
185       return cfg;
186    }
187 
188   private String schemaOutputFile = null;
189   private String delimiter = null;
190 
191 
192 
193   /**
194   * @return
195   */
196   public String getSchemaOutputFile() {
197       return schemaOutputFile;
198    }
199 
200   /**
201   * @return
202   */
203   public String getOutputFile() {
204       return schemaOutputFile;
205    }
206 
207   /**
208   * @return
209   */
210   public String getDelimiter() {
211       return delimiter;
212    }
213 
214   /**
215   * @param string
216   */
217   public void setSchemaOutputFile(String string) {
218       schemaOutputFile = string;
219    }
220 
221   /**
222   * @param string
223   */
224   public void setOutputFile(String string) {
225       schemaOutputFile = string;
226    }
227 
228   /**
229   * @param string
230   */
231   public void setDelimiter(String string) {
232       delimiter = string;
233    }
234 
235 }