summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/binding-ejb-runtime/src/main/java/org/apache/tuscany/sca/binding/ejb/util/NamingEndpoint.java
blob: 346b7c5cf6466ee0b26edd514e24c0ecfcfeb6ea (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.sca.binding.ejb.util;

import java.security.AccessController;
import java.security.PrivilegedAction;

public class NamingEndpoint {
    private String jndiName;
    private EJBLocator locator;
    private boolean managed = true;

    public NamingEndpoint(String hostName, int port, String jndiName) {
        this.jndiName = jndiName;
        this.locator = new EJBLocator(hostName, port);
    }

    public NamingEndpoint(String name) {

        /**
         * by default it's a managed environment means SCA composite with ref
         * binding is running on an AppServer. If running on J2SE, pass
         * -Dmanaged=false for the VM
         */
        final String managedEnv = AccessController.doPrivileged(new PrivilegedAction<String>() {
            public String run() {
                return System.getProperty("managed");
            }
        });

        if (managedEnv != null) {
            managed = Boolean.valueOf(managedEnv);
        }

        if ((!managed) && name.startsWith("corbaname:iiop:")) {
            /**
             * if (name.startsWith("corbaname:iiop:")) { corbaname:iiop:<hostName>:<port>/<root>#name
             * For example,
             * "corbaname:iiop:localhost:2809/NameServiceServerRoot#ejb/MyEJBHome";
             */

            String[] parts = split(name, '#');
            if (parts.length != 2) {
                throw new IllegalArgumentException("Invalid corbaname: " + name);
            }    

            this.jndiName = name; // The logical JNDI name
            this.locator = new EJBLocator(parts[0], managed);

        } else {
            this.jndiName = name;
            this.locator = new EJBLocator(managed);
        }

    }

    private static String[] split(String str, char ch) {
        int index = str.lastIndexOf(ch);
        if (index == -1) {
            return new String[] {str, ""};
        } else {
            return new String[] {str.substring(0, index), str.substring(index + 1)};
        }
    }

    /**
     * @return Returns the jndiName.
     */
    public String getJndiName() {
        return jndiName;
    }

    public EJBLocator getLocator() {
        return locator;
    }

    public String getCorbaname() {
        return locator.getCorbaname(jndiName);
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof NamingEndpoint) {
            NamingEndpoint endpoint = (NamingEndpoint)obj;
            return jndiName.equals(endpoint.jndiName);
        }
        return false;
    }

    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return jndiName.hashCode();
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return jndiName;
    }
}