summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DomainManagerUtil.java
blob: 069cc9f5ae4aaf80a81b664e459e1599955cc8ce (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
/*
 * 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.domain.manager.impl;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.core.assembly.CompositeActivator;
import org.apache.tuscany.sca.core.context.ServiceReferenceImpl;
import org.apache.tuscany.sca.core.invocation.ProxyFactory;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
import org.osoa.sca.ServiceReference;
import org.osoa.sca.ServiceRuntimeException;

/**
 * Common functions and constants used by the admin components.
 *
 * @version $Rev$ $Date$
 */
public final class DomainManagerUtil {

    static final String DEPLOYMENT_CONTRIBUTION_URI = "http://tuscany.apache.org/cloud";

    /**
     * Extracts a qname from a key expressed as contributionURI;namespace;localpart.
     * @param key
     * @return
     */
    static QName compositeQName(String key) {
        int i = key.indexOf(';');
        key = key.substring(i + 1);
        i = key.indexOf(';');
        return new QName(key.substring(0, i), key.substring(i + 1));
    }

    /**
     * Returns a composite title expressed as contributionURI - namespace;localpart.
     * @param qname
     * @return
     */
    static String compositeTitle(String uri, QName qname) {
        if (uri.equals(DEPLOYMENT_CONTRIBUTION_URI)) {
            return qname.getLocalPart();
        } else {
            return uri + " - " + qname.getNamespaceURI() + ";" + qname.getLocalPart();
        }
    }

    /**
     * Extracts a contribution uri from a key expressed as contributionURI;namespace;localpart.
     * @param key
     * @return
     */
    static String contributionURI(String key) {
        int i = key.indexOf(';');
        return key.substring("composite:".length(), i);
    }

    /**
     * Returns a composite key expressed as contributionURI;namespace;localpart.
     * @param qname
     * @return
     */
    static String compositeKey(String uri, QName qname) {
        return "composite:" + uri + ';' + qname.getNamespaceURI() + ';' + qname.getLocalPart();
    }

    /**
     * Returns a link to the source of a composite
     * @param contributionURI
     * @param qname
     * @return
     */
    static String compositeSourceLink(String contributionURI, QName qname) {
        return "/composite-source/" + compositeKey(contributionURI, qname);
    }

    /**
     * Returns a composite title expressed as contributionURI - namespace;localpart.
     * @param qname
     * @return
     */
    static String compositeSimpleTitle(String uri, QName qname) {
        if (uri.equals(DomainManagerUtil.DEPLOYMENT_CONTRIBUTION_URI)) {
            return qname.getLocalPart();
        } else {
            return qname.getNamespaceURI() + ";" + qname.getLocalPart();
        }
    }

    /**
     * Returns a URL from a location string.
     * @param location
     * @return
     * @throws MalformedURLException
     */
    static URL locationURL(String location) throws MalformedURLException {
        String scheme = null; 
        URI uri = null;
        
        IllegalArgumentException uriException = null;
        try {
            uri = URI.create(location);
            scheme = uri.getScheme();
        }catch (java.lang.IllegalArgumentException e) {
            uriException = e;
        }
        
        if (scheme == null) {
            File file = new File(location);
            return file.toURI().toURL();
        } else if (scheme.equals("file")) {
            File file = new File(location.substring(5));
            return file.toURI().toURL();
        } else if(uri == null){
            throw uriException;
        } else {
            return uri.toURL();
        }
    }

    /**
     * Returns a link to a deployable composite.
     * 
     * If the containing contribution is a local directory, return the URI of  the local composite file
     * inside the contribution.
     * 
     * If the containing contribution is a local or remote file, return a URI of the form:
     * jar: contribution URI !/ composite URI.
     *  
     * @param contributionLocation
     * @param deployableURI
     * @return
     */
    static String compositeAlternateLink(String contributionLocation, String deployableURI) {
        if (deployableURI == null) {
            return null;
        }
        URI u = URI.create(contributionLocation);
        String uri;
        if ("file".equals(u.getScheme())) {
            String path = u.toString().substring(5);
            File file = new File(path);
            if (file.isDirectory()) {
                if (contributionLocation.endsWith("/")) {
                    uri = contributionLocation + deployableURI;
                } else {
                    uri = contributionLocation + "/" + deployableURI;
                }
            } else {
                uri = contributionLocation + "!/" + deployableURI; 
            }
        } else {
            uri = contributionLocation + "!/" + deployableURI;
        }
        int e = uri.indexOf("!/"); 
        if (e != -1) {
            int s = uri.lastIndexOf('/', e - 2) +1;
            if (uri.substring(s, e).contains(".")) {
                uri = "jar:" + uri;
            } else {
                uri = uri.substring(0, e) + uri.substring(e + 1);
            }
        }
        return uri;
    }

    /**
     * Extract a node URI from an ATOM entry content.
     * 
     * @param content
     * @return
     */
    static String nodeURI(String content) {
        if (content != null) {
            int bs = content.indexOf("<span id=\"nodeURI\">");
            if (bs != -1) {
                content = content.substring(bs + 19);
                int es = content.indexOf("</span>");
                if (es != -1) {
                    return content.substring(0, es);
                }
            }
        }
        return null;
    }

    /**
     * Create a new service reference dynamically.
     * 
     * @param <B>
     * @param businessInterface
     * @param binding
     * @param assemblyFactory
     * @param compositeActivator
     * @return
     */
    static <B> ServiceReference<B> dynamicReference(Class<B> businessInterface, Binding binding, AssemblyFactory assemblyFactory, CompositeActivator compositeActivator) {
        try {
    
            Composite composite = assemblyFactory.createComposite();
            composite.setName(new QName("http://tuscany.apache.org/xmlns/sca/1.0", "default"));
            RuntimeComponent component = (RuntimeComponent)assemblyFactory.createComponent();
            component.setName("default");
            component.setURI("default");
            compositeActivator.configureComponentContext(component);
            composite.getComponents().add(component);
            RuntimeComponentReference reference = (RuntimeComponentReference)assemblyFactory.createComponentReference();
            reference.setName("default");
            JavaInterfaceFactory javaInterfaceFactory = compositeActivator.getJavaInterfaceFactory();
            InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract();
            interfaceContract.setInterface(javaInterfaceFactory.createJavaInterface(businessInterface));
            reference.setInterfaceContract(interfaceContract);
            component.getReferences().add(reference);
            reference.setComponent(component);
            reference.getBindings().add(binding);
    
            ProxyFactory proxyFactory = compositeActivator.getProxyFactory();
            return new ServiceReferenceImpl<B>(businessInterface, component, reference, binding, proxyFactory, compositeActivator);
            
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
    }

    /**
     * Returns the last modified time of the content at the given URL.
     * 
     * @param url
     * @return
     * @throws IOException
     */
    static long lastModified(URL url) throws IOException {
        
        if (url.getProtocol() == null || "file".equals(url.getProtocol())) {
            return lastModified(new File(url.getPath()));
        } else {
            URLConnection connection = url.openConnection();
            long lastModified = connection.getLastModified();
            return lastModified;
        }
    }

    /**
     * Returns the last modified time of the given file or directory.
     *  
     * @param file
     * @return
     */
    static long lastModified(File file) {
        if (file.isDirectory()) {
            long lastModified = file.lastModified();
            
            for (File child: file.listFiles()) {
                long m = lastModified(child);
                if (m > lastModified) {
                    lastModified = m;
                }
            }
            return lastModified;
            
        } else {
            return file.lastModified();
        }
    }
}