2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.common
;
12 import java
.lang
.reflect
.Constructor
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.util
.HashSet
;
17 import org
.apache
.commons
.lang
.StringUtils
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.hibernate
.SessionFactory
;
20 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
21 import org
.springframework
.transaction
.PlatformTransactionManager
;
22 import org
.springframework
.transaction
.TransactionDefinition
;
23 import org
.springframework
.transaction
.TransactionStatus
;
24 import org
.springframework
.transaction
.support
.DefaultTransactionDefinition
;
26 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationDefaultConfiguration
;
27 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
28 import eu
.etaxonomy
.cdm
.io
.common
.events
.IIoEvent
;
29 import eu
.etaxonomy
.cdm
.io
.common
.events
.IIoObserver
;
30 import eu
.etaxonomy
.cdm
.io
.common
.events
.IoProblemEvent
;
31 import eu
.etaxonomy
.cdm
.io
.common
.events
.IoProgressEvent
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
38 public abstract class CdmIoBase
<STATE
extends IoStateBase
> extends CdmApplicationDefaultConfiguration
39 implements ICdmIO
<STATE
>, IIoObservable
{
40 private static final Logger logger
= Logger
.getLogger(CdmIoBase
.class);
42 private final Set
<IIoObserver
> observers
= new HashSet
<IIoObserver
>();
43 protected String ioName
= null;
51 this.ioName
= this.getClass().getSimpleName();
54 //******************** Observers *********************************************************
57 public boolean addObserver(IIoObserver observer
){
58 return observers
.add(observer
);
62 public Set
<IIoObserver
> getObservers() {
67 public void addObservers(Set
<IIoObserver
> newObservers
) {
68 for (IIoObserver observer
: newObservers
){
69 this.observers
.add(observer
);
74 public int countObservers(){
75 return observers
.size();
79 public boolean removeObserver(IIoObserver observer
){
80 return observers
.remove(observer
);
84 public void removeObservers(){
85 observers
.removeAll(observers
);
89 public void fire(IIoEvent event
){
90 for (IIoObserver observer
: observers
){
91 observer
.handleEvent(event
);
95 //******************** End Observers *********************************************************
98 public int countSteps(){
103 public boolean invoke(STATE state
) {
104 if (isIgnore( state
)){
105 logger
.info("No invoke for " + ioName
+ " (ignored)");
108 updateProgress(state
, "Invoking " + ioName
);
110 return state
.isSuccess();
115 * invoke method to be implemented by implementing classes
119 protected abstract void doInvoke(STATE state
);
122 private SessionFactory sessionFactory
;
125 * flush the current session
127 //TODO move into super class CdmApplicationDefaultConfiguration#flush() ?
128 public void flush() {
129 sessionFactory
.getCurrentSession().flush();
133 //TODO seems to be exact duplicate of CdmApplicationDefaultConfiguration#startTransaction(), remove duplicate
134 public TransactionStatus
startTransaction() {
135 return startTransaction(false);
139 //TODO seems to be exact duplicate of CdmApplicationDefaultConfiguration#startTransaction(java.lang.Boolean)
140 public TransactionStatus
startTransaction(Boolean readOnly
) {
142 DefaultTransactionDefinition defaultTxDef
= new DefaultTransactionDefinition();
143 defaultTxDef
.setReadOnly(readOnly
);
144 TransactionDefinition txDef
= defaultTxDef
;
146 // Log some transaction-related debug information.
147 if (logger
.isDebugEnabled()) {
148 logger
.debug("Transaction name = " + txDef
.getName());
149 logger
.debug("Transaction facets:");
150 logger
.debug("Propagation behavior = " + txDef
.getPropagationBehavior());
151 logger
.debug("Isolation level = " + txDef
.getIsolationLevel());
152 logger
.debug("Timeout = " + txDef
.getTimeout());
153 logger
.debug("Read Only = " + txDef
.isReadOnly());
154 // org.springframework.orm.hibernate4.HibernateTransactionManager
155 // provides more transaction/session-related debug information.
158 TransactionStatus txStatus
= super.getTransactionManager().getTransaction(txDef
);
163 //TODO seems to be exact duplicate of CdmApplicationDefaultConfiguration#startTransaction(java.lang.Boolean), remove duplicate?
164 public void commitTransaction(TransactionStatus txStatus
){
165 PlatformTransactionManager txManager
= super.getTransactionManager();
166 txManager
.commit(txStatus
);
170 //TODO move into super class CdmApplicationDefaultConfiguration#startTransaction(java.lang.Boolean)
172 public void rollbackTransaction(TransactionStatus txStatus
){
173 PlatformTransactionManager txManager
= super.getTransactionManager();
174 txManager
.rollback(txStatus
);
179 public boolean check(STATE state
) {
180 if (isIgnore(state
)){
181 logger
.info("No check for " + ioName
+ " (ignored)");
184 return doCheck(state
);
188 protected abstract boolean doCheck(STATE state
);
192 * Returns true if this (IO-)class should be ignored during the import/export process.
193 * This information is usually stored in the configuration
197 protected abstract boolean isIgnore(STATE state
);
199 protected <T
extends CdmBase
> T
getInstance(Class
<?
extends T
> clazz
){
202 Constructor
<?
extends T
> constructor
= clazz
.getDeclaredConstructor();
203 constructor
.setAccessible(true);
204 result
= constructor
.newInstance();
205 } catch (InstantiationException e
) {
206 logger
.error("Class " + clazz
.getSimpleName()+" could not be instantiated. Class = " );
208 } catch (IllegalAccessException e
) {
209 logger
.error("Constructor of class "+clazz
.getSimpleName()+" could not be accessed." );
211 } catch (SecurityException e
) {
213 } catch (NoSuchMethodException e
) {
214 logger
.error("SecurityException for Constructor of class "+clazz
.getSimpleName()+"." );
216 } catch (IllegalArgumentException e
) {
217 logger
.error("Empty Constructor does not exist for class "+clazz
.getSimpleName()+"." );
219 } catch (InvocationTargetException e
) {
220 logger
.error("Empty Constructor could not be invoked for class "+clazz
.getSimpleName()+"." );
227 protected String
getSuccessString(boolean success
){
229 return "with success";
231 return "with errors";
236 public void updateProgress(STATE state
, String message
) {
237 updateProgress(state
, message
, 1);
241 public void updateProgress(STATE state
, String message
, int worked
) {
242 IProgressMonitor progressMonitor
= state
.getConfig().getProgressMonitor();
243 if(progressMonitor
!= null){
244 progressMonitor
.worked(worked
);
245 progressMonitor
.subTask(message
);
250 public void warnProgress(STATE state
, String message
, Throwable e
) {
251 if(state
.getConfig().getProgressMonitor() != null){
252 IProgressMonitor monitor
= state
.getConfig().getProgressMonitor();
254 monitor
.warning(message
);
256 monitor
.warning(message
, e
);
261 protected void fireProgressEvent(String message
, String location
) {
262 IoProgressEvent event
= new IoProgressEvent();
263 event
.setThrowingClass(this.getClass());
264 event
.setMessage(message
);
265 event
.setLocation(location
);
266 // int linenumber = new Exception().getStackTrace()[0].getLineNumber();
271 protected void fireWarningEvent(String message
, String dataLocation
, Integer severity
) {
272 fireWarningEvent(message
, dataLocation
, severity
, 1);
275 protected void fireWarningEvent(String message
, String dataLocation
, Integer severity
, int stackDepth
) {
277 StackTraceElement
[] stackTrace
= new Exception().getStackTrace();
278 int lineNumber
= stackTrace
[stackDepth
].getLineNumber();
279 String methodName
= stackTrace
[stackDepth
].getMethodName();
280 String className
= stackTrace
[stackDepth
].getClassName();
281 Class
<?
> declaringClass
;
283 declaringClass
= Class
.forName(className
);
284 } catch (ClassNotFoundException e
) {
285 declaringClass
= this.getClass();
288 IoProblemEvent event
= IoProblemEvent
.NewInstance(declaringClass
, message
, dataLocation
,
289 lineNumber
, severity
, methodName
);
291 //for performance improvement one may read:
292 //http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection
293 // Object o = new SecurityManager().getSecurityContext();
299 protected boolean isBlank(String str
){
300 return StringUtils
.isBlank(str
);
303 protected boolean isNotBlank(String str
){
304 return StringUtils
.isNotBlank(str
);