Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../img/srcFileCovDistChart0.png 29% of files have more coverage
33   218   22   3,3
12   94   0,67   10
10     2,2  
1    
14,1% of code in this file is excluded from these metrics.
 
  Domain       Line # 44 33 22 0% 0.0
 
No Tests
 
1    /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10    * This library is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this library; if not, write to the Free Software
17    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18    *
19    * For further informations on the SmartWeb Framework please visit
20    *
21    * http://smartweb.sourceforge.net
22    */
23    package net.smartlab.web;
24   
25    import java.net.URL;
26   
27    import net.smartlab.config.Configuration;
28    import net.smartlab.config.ConfigurationException;
29    import net.smartlab.web.config.DomainConfigurationStrategy;
30    import net.smartlab.web.config.FileDomainConfigurationStrategy;
31   
32    import org.apache.commons.logging.Log;
33    import org.apache.commons.logging.LogFactory;
34    import org.hibernate.HibernateException;
35    import org.hibernate.StaleObjectStateException;
36    import org.hibernate.Transaction;
37   
38    /**
39    * @author rlogiacco
40    * @uml.dependency supplier="net.smartlab.web.BusinessException"
41    * @uml.dependency supplier="net.smartlab.web.BusinessObjectFactory"
42    * @uml.dependency supplier="net.smartlab.web.config.DomainConfigurationStrategy"
43    */
 
44    public abstract class Domain implements ManageableDomain {
45   
46    /**
47    * Provides logging capabilities to the domain.
48    */
49    protected final Log logger = LogFactory.getLog(this.getClass());
50   
51    /**
52    * The internal configuration.
53    */
54    private Configuration config;
55   
56    /**
57    * Strategy to retrieve the configuration file.
58    */
59    private static DomainConfigurationStrategy strategy = new FileDomainConfigurationStrategy();
 
60  0 toggle static {
61  0 try {
62  0 String strategy = System.getProperty("smartweb.domain.strategy");
63  0 if (strategy != null) {
64  0 Domain.strategy = (DomainConfigurationStrategy)Class.forName(strategy).newInstance();
65    } else {
66  0 LogFactory.getLog(Domain.class).warn("No configuration found: falling back to default configuration");
67    }
68    } catch (Exception e) {
69    LogFactory.getLog(Domain.class).fatal("Error configuring SmartWeb", e);
70    }
71    }
72   
73   
74    /**
75    * Starts a new transaction, allowing all the operations performed in the
76    * same context to be atomically applied (commit) or reverted (rollback).
77    * The context is identifyied by a <code>BusinessObjectFactory</code>
78    * instance but the context spans over all the instances sharing the same
79    * configuration.
80    *
81    * @param factory the identifier for the context
82    * @return an object representation of the transaction.
83    * @throws BusinessException if something wrong occurs during the operation.
84    */
 
85  0 toggle protected Transaction begin(BusinessObjectFactory factory) throws BusinessException {
86  0 try {
87  0 return factory.current().beginTransaction();
88    } catch (HibernateException he) {
89    logger.error("[ smartweb ] failed to begin transaction");
90    throw new BusinessException("persistence.error.begin", he);
91    } catch (DAOException boe) {
92    throw new BusinessException(boe);
93    }
94    }
95   
96    /**
97    * Commits the specified transaction applying all the changes happened since
98    * its beginning.
99    *
100    * @param transaction the transaction to commit.
101    * @throws BusinessException if something wrong occurs during the operation.
102    */
 
103  0 toggle protected void commit(Transaction transaction) throws BusinessException {
104  0 try {
105  0 transaction.commit();
106    } catch (StaleObjectStateException sose) {
107    logger.info("[ smartweb ] optimistical locking collision");
108    throw new BusinessException("persistence.locking.collision", sose);
109    } catch (HibernateException he) {
110    logger.error("[ smartweb ] failed to commit transaction");
111    throw new BusinessException("persistence.error.commit", he);
112    }
113    }
114   
115    /**
116    * Rollbacks the specified transaction reverting all the changes happened
117    * since its beginning.
118    *
119    * @param transaction the transaction to rollback.
120    * @throws BusinessException if something wrong occurs during the operation.
121    */
 
122  0 toggle protected void rollback(Transaction transaction) throws BusinessException {
123  0 try {
124  0 transaction.rollback();
125    } catch (HibernateException he) {
126    logger.warn("[ smartweb ] failed to rollback transaction");
127    }
128    }
129   
130    /**
131    * Changes the strategy used to configure the domain.
132    *
133    * @param strategy an implementation of the
134    * <code>DomainConfigurationStrategy</code> interface.
135    */
 
136  0 toggle public static void setConfigurationStrategy(DomainConfigurationStrategy strategy) {
137  0 Domain.strategy = strategy;
138    }
139   
140    /**
141    * Subclasses should make their own constructors private and behave like
142    * singletons.
143    */
 
144  0 toggle protected Domain() {
145  0 if (logger.isDebugEnabled()) {
146  0 logger.debug(this.getClass().getName() + " instantiated.");
147    }
148    }
149   
150    /**
151    * Returns the <code>Configuration</code> instance used by this instance.
152    *
153    * @return the <code>Configuration</code> instance used by this instance.
154    * @throws ConfigurationException if something wrong occurs while reading
155    * the configuration file, usually meaning the configuration file is
156    * missing.
157    */
 
158  0 toggle public Configuration getConfiguration() throws ConfigurationException {
159  0 if (config == null) {
160  0 this.config = strategy.getConfiguration(this);
161    }
162  0 return config;
163    }
164   
165    /**
166    * Returns the <code>Configuration</code> instance used by this instance.
167    *
168    * @param filename the configuration file name.
169    * @return the <code>Configuration</code> instance used by this instance.
170    * @throws ConfigurationException if something wrong occurs while reading
171    * the configuration file, usually meaning the configuration file is
172    * missing.
173    */
 
174  0 toggle public Configuration getConfiguration(String filename) throws ConfigurationException {
175  0 if (config == null) {
176  0 config = strategy.getConfiguration(this, filename);
177    }
178  0 return config;
179    }
180   
181    /**
182    * Returns the first resource in the list available in the specified
183    * context.
184    *
185    * @param context the class identifying the search context.
186    * @param names the list, ordered from first to last, of resources to search
187    * for.
188    * @return the first resource in the list available in the context.
189    */
 
190  0 toggle public static URL getResource(Class context, String[] names) {
191  0 Log logger = LogFactory.getLog(context);
192  0 URL resource = null;
193  0 for (int i = 0; i < names.length; i++) {
194  0 resource = context.getResource(names[i]);
195  0 logger.trace(" trying `" + names[i] + "`");
196  0 if (resource != null) {
197  0 break;
198    }
199    }
200  0 logger.debug(" resource is `" + resource + "`");
201  0 return resource;
202    }
203   
204    /**
205    * Returns the name of the last archive containing the class or an
206    * <code>empty</code> string if the class is in no archive.
207    *
208    * @param type the class to search for.
209    * @return the name of the last archive containing the class or an
210    * <code>empty</code> string if the class is in no archive.
211    */
 
212  0 toggle public static String getLastArchiveName(Class type) {
213  0 Log logger = LogFactory.getLog(type);
214  0 String path = type.getProtectionDomain().getCodeSource().getLocation().getFile();
215  0 logger.debug(" archive path is `" + path + "`");
216  0 return path.substring(path.lastIndexOf('/') + 1);
217    }
218    }