4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
7 * BonYong Lee <bonyong.lee@samsung.com>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 package org.tizen.common.core.command;
27 import static org.tizen.common.core.command.Policy.EXCEPTION_UNHANDLED;
29 import java.lang.Thread.UncaughtExceptionHandler;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.tizen.common.Factory;
40 * Execute command in specific environment
42 * Environment is specified by {@link #contextFactory}
46 * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
52 * Logger for this instance
54 protected Logger logger = LoggerFactory.getLogger( getClass() );
57 * Factory for {@link ExecutionContext}
59 protected Factory<ExecutionContext> contextFactory;
62 * Cabinet for {@link ExecutionContext}
64 * Context is sustain in execution and destroy after exection
66 * @see #contextFactory
68 protected ThreadLocal<ExecutionContext> contexts = new ThreadLocal<ExecutionContext>()
71 * @see java.lang.ThreadLocal#initialValue()
78 if ( null == contextFactory )
82 return contextFactory.create();
95 * Constructor with context factory
97 * @param factory context factory to use
99 * @see #setContextFactory(Factory)
103 final Factory<ExecutionContext> factory
106 setContextFactory( factory );
110 * Return context factory
112 * @return context factory in use
115 Factory<ExecutionContext>
118 return this.contextFactory;
122 * Set context factory
124 * @param factory context factory to use
129 final Factory<ExecutionContext> factory
132 logger.info( "ExecutionContextFactory :{}", factory );
133 this.contextFactory = factory;
137 * Execute <code>commands</code>
139 * @param commands {@link Command}s
144 final Command<?>... commands
147 logger.trace( "Commands :{}", commands );
150 execute( contexts.get(), commands );
156 final ExecutionContext context,
157 final Command<?>... commands
162 for ( final Command<?> command : commands )
164 command.run( this, context );
166 } catch ( final Exception e ) {
167 final Policy policy = context.getPolicy( EXCEPTION_UNHANDLED );
168 if ( null == policy )
170 logger.error( "Error occured", e );
174 final UncaughtExceptionHandler handler = policy.adapt( UncaughtExceptionHandler.class );
175 if ( null == handler )
177 throw new IllegalStateException( e );
179 handler.uncaughtException( Thread.currentThread(), e );
188 * Execute <code>command</code> in <code>context</code>
190 * @param command {@link Command} to run
191 * @param context {@link ExecutionContext} to run in
195 final Command<?> command,
196 final ExecutionContext context
200 command.run( this, context );
204 * Return {@link ExecutionContext} in context
206 * @return {@link ExecutionContext}
208 public ExecutionContext
211 return contexts.get();