summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/SCANodeUtil.java
blob: b4a42dd896f51cda1b6270f4928582dd8de52da8 (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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * 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.node.impl;

import java.util.logging.Logger;


/**
 * Some utility methods for the Node implementation
 * 
 * @version $Rev: 556897 $ $Date: 2007-09-07 12:41:52 +0100 (Fri, 07 Sep 2007) $
 */
public class SCANodeUtil {
	private static final Logger logger = Logger.getLogger(SCANodeUtil.class.getName());
	
    /**
     * Given a contribution path an array of composite names or neither this method finds 
     * a suitable contribution to load
     * 
     * @param classLoader
     * @param compositePath
     * @param composites
     * @return the contribution URL
     * @throws MalformedURLException
     */
/*
    public static URL findContributionURLFromCompositeNameOrPath(ClassLoader classLoader, String contributionPath, String[] composites)
      throws MalformedURLException {
        
        String contributionArtifactPath = null;
        URL contributionArtifactURL = null;
        
        
        if (contributionPath != null && contributionPath.length() > 0) {
            
            //encode spaces as they would cause URISyntaxException
            contributionPath = contributionPath.replace(" ", "%20");
            URI contributionURI = URI.create(contributionPath);
            if (contributionURI.isAbsolute() || composites.length == 0) {
                return new URL(contributionPath);
            } else {
        //        contributionArtifactURL = classLoader.getResource(contributionPath);
        //        if (contributionArtifactURL == null) {
        //            throw new IllegalArgumentException("Composite not found: " + contributionArtifactPath);
        //        }
            }
        }

        if ( contributionArtifactURL == null){
            if (composites != null && composites.length > 0 && composites[0].length() > 0) {
        
                // Here the SCADomain was started with a reference to a composite file
                contributionArtifactPath = composites[0];
                contributionArtifactURL = classLoader.getResource(contributionArtifactPath);
                if (contributionArtifactURL == null) {
                    throw new IllegalArgumentException("Composite not found: " + contributionArtifactPath);
                }
            } else {
        
                // Here the SCANode was started without any reference to a composite file
                // We are going to look for an sca-contribution.xml or sca-contribution-generated.xml
        
                // Look for META-INF/sca-contribution.xml
                contributionArtifactPath = Contribution.SCA_CONTRIBUTION_META;
                contributionArtifactURL = classLoader.getResource(contributionArtifactPath);
        
                // Look for META-INF/sca-contribution-generated.xml
                if (contributionArtifactURL == null) {
                    contributionArtifactPath = Contribution.SCA_CONTRIBUTION_GENERATED_META;
                    contributionArtifactURL = classLoader.getResource(contributionArtifactPath);
                }
        
                // Look for META-INF/sca-deployables directory
                if (contributionArtifactURL == null) {
                    contributionArtifactPath = Contribution.SCA_CONTRIBUTION_DEPLOYABLES;
                    contributionArtifactURL = classLoader.getResource(contributionArtifactPath);
                }
            }
        }
    
        if (contributionArtifactURL == null) {
            throw new IllegalArgumentException("Can't determine contribution deployables. Either specify a composite file, or use an sca-contribution.xml file to specify the deployables.");
        }
    
        URL contributionURL = null;
        // "jar:file://....../something.jar!/a/b/c/app.composite"
        try {
            String url = contributionArtifactURL.toExternalForm();
            String protocol = contributionArtifactURL.getProtocol();
            if ("file".equals(protocol)) {
                // directory contribution
                if (url.endsWith(contributionArtifactPath)) {
                    String location = url.substring(0, url.lastIndexOf(contributionArtifactPath));
                    // workaround from evil url/uri form maven
                    contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL();
                }
    
            } else if ("jar".equals(protocol)) {
                // jar contribution
                String location = url.substring(4, url.lastIndexOf("!/"));
                // workaround for evil url/uri from maven
                contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL();
            }
        } catch (MalformedURLException mfe) {
            throw new IllegalArgumentException(mfe);
        }
    
        return contributionURL;
    }	
*/   
    /** 
     * A rather ugly method to find and fix the URL of the service, assuming that there
     * is one. 
     *  
     * we can't get this out of a service reference
     * the component itself doesn't know how to get it  
     * the binding can't to do it automatically as it's not the sca binding
     * 
     * TODO - This would be better done by passing out a Serializable reference to service discovery 
     *         but this doesn't work yet     
     * 
     * @return node manager URL
     */
/*	
    public static void fixUpNodeServiceUrls(List<Component> nodeComponents, URL nodeUrlString)
      throws MalformedURLException, UnknownHostException, IOException {
      
        for(Component component : nodeComponents){
            for (ComponentService service : component.getServices() ){
                for (Binding binding : service.getBindings() ) {
                    fixUpNodeServiceBindingUrl(binding, nodeUrlString);  
                }
            }            
        }
    }   
*/
    /**
     * Find and return the URL of the NodeManagerService
     * 
     * @param nodeComponents
     * @return
     */
/*
    public static String getNodeManagerServiceUrl(List<Component> nodeComponents){
        String nodeManagerUrl = null;
              
        for(Component component : nodeComponents){
            for (ComponentService service : component.getServices() ){
                
                if ( service.getName().equals("NodeManagerService")) {
                    nodeManagerUrl = service.getBindings().get(0).getURI();
                }
            }            
        }
        
        return nodeManagerUrl;
    } 
*/   
    
    /**
     * For node management services that use the HTTP(S) protocol then use the node URL as the enpoint
     * if it has been specified otherwise find a port that isn't in use and make sure the domain name 
     * is the real domain name
     * 
     * @param binding
     * @param nodeURL the URL provided as the identifier of the node
     */
/*
    public static void fixUpNodeServiceBindingUrl(Binding binding, URL manualUrl)
      throws MalformedURLException, UnknownHostException, IOException {

        String urlString = binding.getURI(); 
        
        // only going to fiddle with bindings that use HTTP protocol
        if( (urlString == null) ||
            ((urlString.startsWith("http") != true ) &&
             (urlString.startsWith("https") != true )) ||
            (binding instanceof SCABinding)) {
            return;
        }
        
        URL bindingUrl =  new URL(urlString);
        String originalHost = bindingUrl.getHost();
        String newHost = null;
        int originalPort = bindingUrl.getPort();
        int newPort = 0;
        
        if (manualUrl != null) {
            // the required url has been specified manually
            newHost = manualUrl.getHost();
            newPort = manualUrl.getPort();
            
            if ( newHost.equals("localhost")){
                newHost = InetAddress.getLocalHost().getHostName();
            }
        } else {
            // discover the host and port information
            newHost = InetAddress.getLocalHost().getHostName();
            newPort = findFreePort(originalPort);
        }
        
        // replace the old with the new
        urlString = urlString.replace(String.valueOf(originalPort), String.valueOf(newPort));          
        urlString = urlString.replace(originalHost, newHost);
        
        // set the address back into the NodeManager binding.
        binding.setURI(urlString);   
    }  
*/    
    /**
     * Find a port on this machine that isn't in use. 
     * 
     * @param startPort
     * @return
     */
/*
    public static int findFreePort(int startPort) throws IOException
    {
        ServerSocket socket = new ServerSocket(0);
        int port = socket.getLocalPort();
        socket.close();
        return port;
    }
*/  
    
    /**
     * For node services that have to talk to the domain fix up the reference URL using the 
     * provided domain URL if it has been provided
     * 
     * @param nodeComponents
     * @param domainUrlString
     * @throws MalformedURLException
     * @throws UnknownHostException
     */
/*    
    public static void fixUpNodeReferenceUrls(List<Component> nodeComponents, URL domainUrl)
    throws MalformedURLException, UnknownHostException, ActivationException{
            
      for(Component component : nodeComponents){
          for (ComponentReference reference : component.getReferences() ){
              if ( reference.getName().equals("domainManager") ||
                   reference.getName().equals("scaDomainService")) {
                  for (Binding binding : reference.getBindings() ) {
                      fixUpNodeReferenceBindingUrl(binding, domainUrl);  
                  }
              }
          }            
       }
    }   
*/
    /**
     * For node management references to the domain fix up the binding URLs so that they point
     * to the endpoint described in the domainURL
     * 
     * @param binding
     * @param nodeURL the URL provided as the identifier of the node
     */
/*    
    public static void fixUpNodeReferenceBindingUrl(Binding binding, URL manualUrl)
      throws MalformedURLException, UnknownHostException, ActivationException{

        String urlString = binding.getURI();
        
        // only going to fiddle with bindings that use HTTP protocol
        if( (urlString == null) ||
            ((urlString.startsWith("http") != true ) &&
             (urlString.startsWith("https") != true )) ||
            (binding instanceof SCABinding) ) {
            return;
        }
        
        URL bindingUrl =  new URL(urlString);
        String originalHost = bindingUrl.getHost();
        String newHost = null;
        int originalPort = bindingUrl.getPort();
        int newPort = 0;
        
        if (manualUrl != null) {
            // the required url has been specified manually
            newHost = manualUrl.getHost();
            newPort = manualUrl.getPort();
        } else {
            throw new ActivationException("domain uri can't be null");
        }
        
        // replace the old with the new
        urlString = urlString.replace(String.valueOf(originalPort), String.valueOf(newPort));          
        urlString = urlString.replace(originalHost, newHost);
        
        // set the address back into the NodeManager binding.
        binding.setURI(urlString);   
    } 
    */   
}