Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common / src / org / tizen / common / core / command / Executor.java
1 /*
2  * Common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * BonYong Lee <bonyong.lee@samsung.com>
8  * 
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Contributors:
22  * - S-Core Co., Ltd
23  *
24  */
25 package org.tizen.common.core.command;
26
27 import static org.tizen.common.core.command.Policy.EXCEPTION_UNHANDLED;
28
29 import java.lang.Thread.UncaughtExceptionHandler;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.tizen.common.Factory;
34
35
36 /**
37  * <p>
38  * Executor.
39  * 
40  * Execute command in specific environment
41  * 
42  * Environment is specified by {@link #contextFactory}
43  * 
44  * </p>
45  * 
46  * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
47  */
48 public class
49 Executor
50 {
51         /**
52          * Logger for this instance
53          */
54         protected Logger logger = LoggerFactory.getLogger( getClass() );
55         
56         /**
57          * Factory for {@link ExecutionContext}
58          */
59         protected Factory<ExecutionContext> contextFactory;
60         
61         /**
62          * Cabinet for {@link ExecutionContext}
63          * 
64          * Context is sustain in execution and destroy after exection
65          * 
66          * @see #contextFactory
67          */
68         protected ThreadLocal<ExecutionContext> contexts = new ThreadLocal<ExecutionContext>()
69         {
70                 /* (non-Javadoc)
71                  * @see java.lang.ThreadLocal#initialValue()
72                  */
73                 @Override
74                 protected
75                 ExecutionContext
76                 initialValue()
77                 {
78                         if ( null == contextFactory )
79                         {
80                                 return null;
81                         }
82                         return contextFactory.create();
83                 };
84         };
85         
86         /**
87          * Default constructor
88          */
89         public
90         Executor()
91         {
92         }
93         
94         /**
95          * Constructor with context factory
96          * 
97          * @param factory context factory to use
98          * 
99          * @see #setContextFactory(Factory)
100          */
101         public
102         Executor(
103                 final Factory<ExecutionContext> factory
104         )
105         {
106                 setContextFactory( factory );
107         }
108         
109         /**
110          * Return context factory
111          * 
112          * @return context factory in use
113          */
114         public
115         Factory<ExecutionContext>
116         getContextFactory()
117         {
118                 return this.contextFactory;
119         }
120         
121         /**
122          * Set context factory
123          * 
124          * @param factory context factory to use
125          */
126         public
127         void
128         setContextFactory(
129                 final Factory<ExecutionContext> factory
130         )
131         {
132                 logger.info( "ExecutionContextFactory :{}", factory );
133                 this.contextFactory = factory;
134         }
135         
136         /**
137          * Execute <code>commands</code>
138          * 
139          * @param commands {@link Command}s
140          */
141         public
142         void
143         execute(
144                 final Command<?>... commands
145         )
146         {
147         logger.trace( "Commands :{}", commands );
148         contexts.remove();
149         
150         execute( contexts.get(), commands );
151             
152         }
153         public
154         void
155         execute(
156                 final ExecutionContext context,
157                 final Command<?>... commands
158                 )
159         {
160
161                 try {
162                         for ( final Command<?> command : commands )
163                         {
164                                 command.run( this, context );
165                         }
166                 } catch ( final Exception e ) {
167                         final Policy policy = context.getPolicy( EXCEPTION_UNHANDLED );
168                         if ( null == policy )
169                         {       // No policy
170                                 logger.error( "Error occured", e );
171                                 return ;
172                         }
173                         
174                         final UncaughtExceptionHandler handler = policy.adapt( UncaughtExceptionHandler.class );
175                         if ( null == handler )
176                         {
177                                 throw new IllegalStateException( e );
178                         }
179                         handler.uncaughtException( Thread.currentThread(), e );
180                 }
181                 finally
182                 {
183                 contexts.remove();
184                 }
185         }
186         
187         /**
188          * Execute <code>command</code> in <code>context</code>
189          * 
190          * @param command {@link Command} to run
191          * @param context {@link ExecutionContext} to run in
192          */
193         public void
194         run(
195                 final Command<?> command,
196                 final ExecutionContext context
197         )
198         throws Exception
199         {
200                 command.run( this, context );
201         }
202         
203         /**
204          * Return {@link ExecutionContext} in context
205          * 
206          * @return {@link ExecutionContext}
207          */
208         public ExecutionContext
209         getContext()
210         {
211                 return contexts.get();
212         }
213 }