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
|
/*
* 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.contribution.processor;
import java.net.URI;
import java.net.URL;
import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
import org.apache.tuscany.sca.contribution.service.ContributionReadException;
import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
import org.apache.tuscany.sca.contribution.service.UnrecognizedElementException;
/**
* Implementation of an extensible URL artifact processor.
*
* Takes a URLArtifactProcessorExtensionPoint and delegates to the proper URLArtifactProcessor
* by either fileName or fileExtention
*
* @version $Rev: 616114 $ $Date: 2008-01-28 16:05:37 -0800 (Mon, 28 Jan 2008) $
*/
public class ExtensibleURLArtifactProcessor
implements URLArtifactProcessor<Object> {
private URLArtifactProcessorExtensionPoint processors;
/**
* Constructs a new ExtensibleURLArtifactProcessor.
*
* @param processors
*/
public ExtensibleURLArtifactProcessor(URLArtifactProcessorExtensionPoint processors) {
this.processors = processors;
}
@SuppressWarnings("unchecked")
public Object read(URL contributionURL, URI sourceURI, URL sourceURL) throws ContributionReadException {
URLArtifactProcessor<Object> processor = null;
// Delegate to the processor associated with file extension
String fileName = getFileName(sourceURL);
//try to retrieve a processor for the specific filename
processor = (URLArtifactProcessor<Object>)processors.getProcessor(fileName);
if (processor == null) {
//try to find my file type (extension)
String extension = sourceURL.getPath();
int extensionStart = extension.lastIndexOf('.');
//handle files without extension (e.g NOTICE)
if (extensionStart > 0) {
extension = extension.substring(extensionStart);
processor = (URLArtifactProcessor<Object>)processors.getProcessor(extension);
}
}
if (processor == null) {
return null;
}
return processor.read(contributionURL, sourceURI, sourceURL);
}
@SuppressWarnings("unchecked")
public void resolve(Object model, ModelResolver resolver) throws ContributionResolveException {
// Delegate to the processor associated with the model type
if (model != null) {
URLArtifactProcessor processor = processors.getProcessor(model.getClass());
if (processor != null) {
processor.resolve(model, resolver);
}
}
}
public <M> M read(URL contributionURL, URI artifactURI, URL artifactUrl, Class<M> type)
throws ContributionReadException {
Object mo = read(contributionURL, artifactURI, artifactUrl);
if (type.isInstance(mo)) {
return type.cast(mo);
} else {
UnrecognizedElementException e = new UnrecognizedElementException(null);
e.setResourceURI(artifactURI.toString());
throw e;
}
}
public String getArtifactType() {
return null;
}
public Class<Object> getModelType() {
return null;
}
/**
* Returns the file name from a URL.
* @param url
* @return
*/
private static String getFileName(URL url){
String fileName = url.getPath();
int pos = fileName.lastIndexOf("/");
return fileName.substring(pos +1);
}
}
|