Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79ec710e77246e9a89e3409175a16085ef05fb79 (plain) (blame)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.plus.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Locale;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.eclipse.jetty.util.IntrospectionUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * Injection
 *
 * Represents the injection of a resource into a target (method or field).
 * The injection is performed by doing an ENC lookup using the jndi
 * name provided, and setting the object obtained on the target.
 *
 */
public class Injection
{
    private static final Logger LOG = Log.getLogger(Injection.class);

    private Class<?> _targetClass;
    private String _jndiName;
    private String _mappingName;
    private Member _target;
    private Class<?> _paramClass;
    private Class<?> _resourceClass;

    
    public Injection ()
    {
    }
    

    /**
     * @return the _className
     */
    public Class<?> getTargetClass()
    {
        return _targetClass;
    }

    public Class<?> getParamClass ()
    {
        return _paramClass;
    }
   
    public Class<?> getResourceClass ()
    {
        return _resourceClass;
    }
    
    public boolean isField ()
    {
        return (_target != null && _target instanceof Field);
    }
    
    public boolean isMethod ()
    {
        return (_target != null && _target instanceof Method);
    }
    
    /**
     * @return the jndiName
     */
    public String getJndiName()
    {
        return _jndiName;
    }
    /**
     * @param jndiName the jndiName to set
     */
    public void setJndiName(String jndiName)
    {
        this._jndiName = jndiName;
    }
    /**
     * @return the mappingName
     */
    public String getMappingName()
    {
        return _mappingName;
    }
    /**
     * @param mappingName the mappingName to set
     */
    public void setMappingName(String mappingName)
    {
        this._mappingName = mappingName;
    }
    
    /**
     * @return the target
     */
    public Member getTarget()
    {
        return _target;
    }
    

    public void setTarget(Class<?> clazz, Field field, Class<?> resourceType)
    {
        _targetClass = clazz;
        _target = field;
        _resourceClass = resourceType;
    }
    
    public void setTarget(Class<?> clazz, Method method, Class<?> arg, Class<?> resourceType)
    {
        _targetClass = clazz;
        _target = method;
        _resourceClass = resourceType;
        _paramClass = arg;
    }
   
    public void setTarget (Class<?> clazz, String target, Class<?> resourceType)
    {
        _targetClass = clazz;
        _resourceClass = resourceType;
        
        //first look for a javabeans style setter matching the targetName
        String setter = "set"+target.substring(0,1).toUpperCase(Locale.ENGLISH)+target.substring(1);
        try
        {
            LOG.debug("Looking for method for setter: "+setter+" with arg "+_resourceClass);
            _target = IntrospectionUtil.findMethod(clazz, setter, new Class[] {_resourceClass}, true, false);
            _targetClass = clazz;
            _paramClass = _resourceClass;
        }
        catch (NoSuchMethodException me)
        {
            //try as a field
            try
            {
                _target = IntrospectionUtil.findField(clazz, target, resourceType, true, false);
                _targetClass = clazz;
            }
            catch (NoSuchFieldException fe)
            {
                throw new IllegalArgumentException("No such field or method "+target+" on class "+_targetClass);
            }
        }

    }
    
    /**
     * Inject a value for a Resource from JNDI into an object
     * @param injectable
     */
    public void inject (Object injectable)
    { 
        if (_target != null)
        {
            if (_target instanceof Field)
                injectField((Field)_target, injectable);
            else
                injectMethod((Method)_target, injectable);
        }
        else
            throw new IllegalStateException ("No method or field to inject with "+getJndiName());
    }

    
    /**
     * The Resource must already exist in the ENC of this webapp.
     * @return the injected valud
     * @throws NamingException
     */
    public Object lookupInjectedValue ()
    throws NamingException
    {
        InitialContext context = new InitialContext();
        return context.lookup("java:comp/env/"+getJndiName());
    }
    


    /**
     * Inject value from jndi into a field of an instance
     * @param field
     * @param injectable
     */
    protected void injectField (Field field, Object injectable)
    {        
        try
        {
            boolean accessibility = field.isAccessible();
            field.setAccessible(true);
            field.set(injectable, lookupInjectedValue());
            field.setAccessible(accessibility);
        }
        catch (Exception e)
        {
            LOG.warn(e);
            throw new IllegalStateException("Inject failed for field "+field.getName());
        }
    }

    /**
     * Inject value from jndi into a setter method of an instance
     * @param method
     * @param injectable
     */
    protected void injectMethod (Method method, Object injectable)
    {
        try
        {
            boolean accessibility = method.isAccessible();
            method.setAccessible(true);
            method.invoke(injectable, new Object[] {lookupInjectedValue()});
            method.setAccessible(accessibility);
        }
        catch (Exception e)
        {
            LOG.warn(e);
            throw new IllegalStateException("Inject failed for method "+method.getName());
        }
    }
   
}

Back to the top