summaryrefslogtreecommitdiffstats
path: root/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/context/JavaComponentContext.java
blob: 0b4a800f1f3a276a68e4400ed2643a3bc300dd66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.container.java.context;

import java.util.Iterator;

import org.apache.tuscany.core.builder.ObjectFactory;
import org.apache.tuscany.core.context.AbstractContext;
import org.apache.tuscany.core.context.Context;
import org.apache.tuscany.core.context.ContextInitException;
import org.apache.tuscany.core.context.LifecycleEventListener;
import org.apache.tuscany.core.context.QualifiedName;
import org.apache.tuscany.core.context.SimpleComponentContext;
import org.apache.tuscany.core.context.TargetException;
import org.apache.tuscany.core.injection.EventInvoker;
import org.apache.tuscany.core.injection.Injector;
import org.apache.tuscany.core.injection.ObjectCallbackException;
import org.apache.tuscany.core.injection.ObjectCreationException;

/**
 * Manages Java service component implementation instances
 * 
 * @version $Rev$ $Date$
 */
public class JavaComponentContext extends AbstractContext implements SimpleComponentContext {

    private boolean eagerInit;

    private EventInvoker initInvoker;

    private EventInvoker destroyInvoker;

    private Injector componentName;

    private Injector moduleContext;

    private boolean stateless;

    // the cached target instance
    private Object cachedTargetInstance;

    // creates a new implementation instance with injected references and properties
    private ObjectFactory objectFactory;

    // ----------------------------------
    // Constructors
    // ----------------------------------

    public JavaComponentContext(String name, ObjectFactory objectFactory, boolean eagerInit, EventInvoker initInvoker,
            EventInvoker destroyInvoker, boolean stateless) {
        super(name);
        assert (objectFactory != null) : "Object factory was null";
        if (eagerInit == true && initInvoker == null) {
            ContextInitException e = new ContextInitException("No intialization method found for implementation");
            e.setIdentifier(getName());
            throw e;
        }
        this.objectFactory = objectFactory;

        this.eagerInit = eagerInit;
        this.initInvoker = initInvoker;
        this.destroyInvoker = destroyInvoker;
        this.stateless = stateless;
    }

    // ----------------------------------
    // Methods
    // ----------------------------------

    public void setName(String name) {
        super.setName(name);
    }

    protected int type;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public synchronized Object getInstance(QualifiedName qName) throws TargetException {
        return getInstance(qName, true);
    }

    public synchronized Object getInstance(QualifiedName qName, boolean notify) throws TargetException {
        //TODO implement returning of proxy and invocation chain for service
        if (cachedTargetInstance != null) {
            return cachedTargetInstance; // already cached, just return
        }

        if (getLifecycleState() == ERROR || getLifecycleState() == CONFIG_ERROR) {
            return null;
        }
        synchronized (this) {
            try {
                Object instance = objectFactory.getInstance();
                startInstance(instance);
                if (notify) {
                    for (Iterator iter = contextListener.iterator(); iter.hasNext();) {
                        LifecycleEventListener listener = (LifecycleEventListener) iter.next();
                        listener.onInstanceCreate(this);
                    }
                }
                setLifecycleState(RUNNING);
                if (stateless) {
                    return instance;
                } else {
                    // cache the actual instance
                    cachedTargetInstance = instance;
                    return cachedTargetInstance;
                }
            } catch (ObjectCreationException e) {
                setLifecycleState(Context.ERROR);
                TargetException te = new TargetException("Error creating instance for component", e);
                te.setIdentifier(getName());
                throw te;
            }
        }

    }

    public Object getImplementationInstance() throws TargetException{
        //TODO refactor when getInstance() returns a proxy
        return getInstance(null);
    }

    public Object getImplementationInstance(boolean notify) throws TargetException{
        //TODO refactor when getInstance() returns a proxy
        return getInstance(null,notify);
    }
    

    public boolean isEagerInit() {
        return eagerInit;
    }

    public boolean isDestroyable() {
        return (destroyInvoker != null);
    }

    // ----------------------------------
    // Lifecycle methods
    // ----------------------------------

    public void start() throws ContextInitException {
        if (getLifecycleState() != UNINITIALIZED && getLifecycleState() != STOPPED) {
            throw new IllegalStateException("Component must be in UNINITIALIZED state [" + getLifecycleState() + "]");
        }
        if (objectFactory == null) {
            setLifecycleState(ERROR);
            ContextInitException e = new ContextInitException("Object factory not found");
            e.setIdentifier(getName());
            throw e;
        }
        setLifecycleState(INITIALIZED);
    }

    public void stop() {
        if (cachedTargetInstance != null) {
            if (destroyInvoker != null) {
                try {
                    destroyInvoker.invokeEvent(cachedTargetInstance);
                } catch (ObjectCallbackException e) {
                    TargetException te = new TargetException(e.getCause());
                    te.setIdentifier(getName());
                    throw te;
                }
            }
        }
        setLifecycleState(STOPPED);
    }

    // ----------------------------------
    // Private methods
    // ----------------------------------
    private void startInstance(Object instance) throws TargetException {
        try {
            // handle @Init
            if (initInvoker != null) {
                initInvoker.invokeEvent(instance);
            }
        } catch (ObjectCallbackException e) {
            TargetException te = new TargetException("Error initializing instance", e);
            te.setIdentifier(getName());
            throw te;
        }
    }

}