1    /*
2     * Copyright 2008 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.configuration;
19   
20   import java.io.File;
21   import java.io.Serializable;
22   
23   import javax.xml.bind.annotation.XmlRootElement;
24   import javax.xml.bind.annotation.XmlTransient;
25   
26   import org.torweg.pulse.bundle.Bundle;
27   import org.torweg.pulse.invocation.lifecycle.Lifecycle;
28   
29   /**
30    * is the base class of all {@code Configuration}s.
31    * 
32    * @author Thomas Weber
33    * @version $Revision: 1387 $
34    */
35   @XmlRootElement(name = "configuration")
36   public class Configuration implements Serializable {
37   
38       /**
39        * serialVersionUID.
40        */
41       private static final long serialVersionUID = 5307803483069743990L;
42   
43       /**
44        * the name of the bundle.
45        */
46       @XmlTransient
47       private String bundleName = null;
48   
49       /**
50        * the reference to the config file.
51        */
52       @XmlTransient
53       private File configFileReference;
54   
55       /**
56        * the last modified.
57        */
58       @XmlTransient
59       private long lastModified;
60   
61       /**
62        * sets the {@code Bundle} the {@code Configuration} belongs to.
63        * 
64        * @param bundle
65        *            the bundle
66        * @return the instance
67        */
68       protected final Configuration setBundle(final Bundle bundle) {
69           this.bundleName = bundle.getName();
70           return this;
71       }
72   
73       /**
74        * sets the config file used to unmarshal the configuration.
75        * 
76        * @param cf
77        *            the file
78        * @return the instance
79        */
80       protected final Configuration setConfigFile(final File cf) {
81           this.configFileReference = cf;
82           this.lastModified = cf.lastModified();
83           return this;
84       }
85   
86       /**
87        * returns the {@code Bundle} the configuration belongs to, or
88        * {@code null} if it is a main configuration.
89        * 
90        * @return the bundle or {@code null}
91        */
92       public final Bundle getBundle() {
93           if (bundleName != null) {
94               return Lifecycle.getBundle(this.bundleName);
95           }
96           return null;
97       }
98   
99       /**
100       * returns {@code true}, if the {@code Configuration} belongs to a
101       * {@code Bundle}.
102       * 
103       * @return {@code true}, if the {@code Configuration} belongs to a
104       *         {@code Bundle}
105       */
106      public final boolean isBundleConfiguration() {
107          return (this.bundleName != null);
108      }
109  
110      /**
111       * returns {@code true}, if the configuration has to be reloaded.
112       * 
113       * @return {@code true}, if the configuration has to be reloaded
114       */
115      public final boolean isModified() {
116          return (this.lastModified != this.configFileReference.lastModified());
117      }
118  
119  }
120