~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/transaction/manager/selector/JndiSelector.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  Copyright Terracotta, Inc.
 
3
 *
 
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
5
 *  you may not use this file except in compliance with the License.
 
6
 *  You may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 *  Unless required by applicable law or agreed to in writing, software
 
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 *  See the License for the specific language governing permissions and
 
14
 *  limitations under the License.
 
15
 */
 
16
package net.sf.ehcache.transaction.manager.selector;
 
17
 
 
18
import org.slf4j.Logger;
 
19
import org.slf4j.LoggerFactory;
 
20
 
 
21
import javax.naming.InitialContext;
 
22
import javax.naming.NamingException;
 
23
import javax.transaction.TransactionManager;
 
24
 
 
25
/**
 
26
 * Abstract selector performing JNDI lookup
 
27
 *
 
28
 * @author Ludovic Orban
 
29
 */
 
30
public abstract class JndiSelector extends Selector {
 
31
    private static final Logger LOG = LoggerFactory.getLogger(JndiSelector.class);
 
32
 
 
33
    private volatile String jndiName;
 
34
 
 
35
    /**
 
36
     * Constructor
 
37
     *
 
38
     * @param vendor the transaction manager vendor name
 
39
     * @param jndiName the JNDI name to look up
 
40
     */
 
41
    public JndiSelector(String vendor, String jndiName) {
 
42
        super(vendor);
 
43
        this.jndiName = jndiName;
 
44
    }
 
45
 
 
46
    /**
 
47
     * Get the configured JNDI name to look up
 
48
     * @return the JNDI name to look up
 
49
     */
 
50
    public String getJndiName() {
 
51
        return jndiName;
 
52
    }
 
53
 
 
54
    /**
 
55
     * Set the configured JNDI name to look up
 
56
     * @param jndiName the JNDI name to look up
 
57
     */
 
58
    public void setJndiName(String jndiName) {
 
59
        this.jndiName = jndiName;
 
60
    }
 
61
 
 
62
    /**
 
63
     * {@inheritDoc}
 
64
     */
 
65
    @Override
 
66
    protected TransactionManager doLookup() {
 
67
        InitialContext initialContext;
 
68
        try {
 
69
            initialContext = new InitialContext();
 
70
        } catch (NamingException ne) {
 
71
            LOG.debug("cannot create initial context", ne);
 
72
            return null;
 
73
        }
 
74
 
 
75
        try {
 
76
            Object jndiObject;
 
77
            try {
 
78
                jndiObject = initialContext.lookup(getJndiName());
 
79
                if (jndiObject instanceof TransactionManager) {
 
80
                    return (TransactionManager) jndiObject;
 
81
                }
 
82
            } catch (NamingException e) {
 
83
                LOG.debug("Couldn't locate TransactionManager for {} under {}", getVendor(), getJndiName());
 
84
            }
 
85
            return null;
 
86
        } finally {
 
87
            try {
 
88
                initialContext.close();
 
89
            } catch (NamingException ne) {
 
90
                LOG.warn("error closing initial context", ne);
 
91
            }
 
92
        }
 
93
    }
 
94
}
 
 
b'\\ No newline at end of file'