summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaServiceBindingProvider.java
blob: 5292b3e452b0e7bec6e326baba297d2e4bd83d95 (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
/*
 * 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.corba.impl;

import org.apache.tuscany.sca.binding.corba.CorbaBinding;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.provider.ServiceBindingProvider;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.Servant;
import org.osoa.sca.ServiceRuntimeException;

/**
 * @version $Rev$ $Date$
 */
public class CorbaServiceBindingProvider implements ServiceBindingProvider {
    private CorbaBinding binding;

    /**
     * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#getBindingInterfaceContract()
     */
    public InterfaceContract getBindingInterfaceContract() {
        // TODO Auto-generated method stub
        return null;
    }

    protected Servant createServant() {
        return null;
    }

    private NameComponent[] nameComponents;

    /**
     * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#start()
     */
    public void start() {
        try {
            java.util.Properties props = new java.util.Properties();
            props.put("org.omg.CORBA.ORBInitialHost", binding.getHost());
            props.put("org.omg.CORBA.ORBInitialPort", String.valueOf(binding.getPort()));
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[0], props);

            Servant servant = createServant();
            
            // get reference to rootpoa & activate the POAManager
            POA rootpoa = POAHelper.narrow(
              orb.resolve_initial_references("RootPOA"));
            rootpoa.the_POAManager().activate();

            // get object reference from the servant
            org.omg.CORBA.Object ref = 
              rootpoa.servant_to_reference(servant);
            
            org.omg.CORBA.Object href = null; // AddHelper.narrow(ref);
                
            // get the root naming context
            // NameService invokes the name service
            org.omg.CORBA.Object objRef = 
              orb.resolve_initial_references("NameService");
            // Use NamingContextExt which is part of the Interoperable
            // Naming Service (INS) specification.
            NamingContextExt ncRef = 
              NamingContextExtHelper.narrow(objRef);

            // bind the Object Reference in Naming
            NameComponent path[] = ncRef.to_name( binding.getURI() );
            ncRef.rebind(path, href);

            // wait for invocations from clients
            orb.run();


        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }

    }

    /**
     * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#stop()
     */
    public void stop() {
        try {
            java.util.Properties props = new java.util.Properties();
            props.put("org.omg.CORBA.ORBInitialHost", binding.getHost());
            props.put("org.omg.CORBA.ORBInitialPort", String.valueOf(binding.getPort()));
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[0], props);

            org.omg.CORBA.Object object = orb.resolve_initial_references("NameService");
            org.omg.CosNaming.NamingContextExt root = org.omg.CosNaming.NamingContextExtHelper.narrow(object);
            root.unbind(nameComponents);
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }

    }

    /**
     * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#supportsOneWayInvocation()
     */
    public boolean supportsOneWayInvocation() {
        // TODO Auto-generated method stub
        return false;
    }

}