Package: RepositoryDeployUriNormaliser

RepositoryDeployUriNormaliser

nameinstructionbranchcomplexitylinemethod
RepositoryDeployUriNormaliser(Repository, EventLogger)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
extractTypeNameAndVersion(URI)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lookupArtifactDescriptor(String, String, String, URI)
M: 0 C: 89
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
normalise(URI)
M: 2 C: 9
82%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
normaliseRepositoryUri(URI)
M: 0 C: 66
100%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 0 C: 13
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.kernel.deployer.core.internal.uri;
13:
14: import java.net.URI;
15:
16: import org.osgi.framework.Version;
17:
18:
19: import org.eclipse.virgo.nano.deployer.api.core.DeployUriNormaliser;
20: import org.eclipse.virgo.nano.deployer.api.core.DeployerLogEvents;
21: import org.eclipse.virgo.nano.deployer.api.core.DeploymentException;
22: import org.eclipse.virgo.nano.serviceability.NonNull;
23: import org.eclipse.virgo.medic.eventlog.EventLogger;
24: import org.eclipse.virgo.repository.ArtifactDescriptor;
25: import org.eclipse.virgo.repository.Repository;
26: import org.eclipse.virgo.util.osgi.manifest.VersionRange;
27:
28: /**
29: * A {@link DeployUriNormaliser} implementation that works with a {@link Repository}. Uris in the form
30: * repository://type/name/version are normalised by querying the <code>Repository</code> for a matching
31: * {@link ArtifactDescriptor} and, if one is found, returning its {@link ArtifactDescriptor#getUri() URI}.
32: *
33: * <p />
34: *
35: * <strong>Concurrent Semantics</strong><br />
36: *
37: * Thread-safe.
38: *
39: */
40: final class RepositoryDeployUriNormaliser implements DeployUriNormaliser {
41:
42: private static final String SCHEME_REPOSITORY = "repository";
43:
44: private static final int COMPONENTS_MINIMUM = 2;
45:
46: private static final int COMPONENTS_MAXIMUM = 3;
47:
48: private final Repository repository;
49:
50: private final EventLogger eventLogger;
51:
52: RepositoryDeployUriNormaliser(@NonNull Repository repository, EventLogger eventLogger) {
53: this.repository = repository;
54: this.eventLogger = eventLogger;
55: }
56:
57: public URI normalise(URI uri) throws DeploymentException {
58:• if (SCHEME_REPOSITORY.equals(uri.getScheme())) {
59: return normaliseRepositoryUri(uri);
60: }
61: return null;
62: }
63:
64: private URI normaliseRepositoryUri(URI uri) throws DeploymentException {
65: String[] tnv = extractTypeNameAndVersion(uri);
66:
67: URI normalisedUri = null;
68:• if (tnv.length >= COMPONENTS_MINIMUM && tnv.length <= COMPONENTS_MAXIMUM) {
69: String versionString = null;
70:• if (tnv.length == 3) {
71: versionString = tnv[2];
72: }
73: ArtifactDescriptor artifactDescriptor = lookupArtifactDescriptor(tnv[0], tnv[1], versionString, uri);
74:• if (artifactDescriptor != null) {
75: normalisedUri = artifactDescriptor.getUri();
76: }
77: } else {
78: this.eventLogger.log(DeployerLogEvents.REPOSITORY_DEPLOYMENT_URI_MALFORMED, uri);
79: throw new DeploymentException("The URI '" + uri + "' is malformed");
80: }
81: return normalisedUri;
82: }
83:
84: private String[] extractTypeNameAndVersion(URI uri) {
85: String tnv = uri.getSchemeSpecificPart();
86: String[] tnvComponents = tnv.split("/");
87: return tnvComponents;
88: }
89:
90: private ArtifactDescriptor lookupArtifactDescriptor(String type, String name, String versionString, URI uri) throws DeploymentException {
91: try {
92: VersionRange versionRange;
93:
94:• if (versionString == null) {
95: versionRange = VersionRange.NATURAL_NUMBER_RANGE;
96: } else {
97: Version version = new Version(versionString);
98: versionRange = VersionRange.createExactRange(version);
99: }
100:
101: ArtifactDescriptor artifactDescriptor = this.repository.get(type, name, versionRange);
102:• if (artifactDescriptor == null) {
103: this.eventLogger.log(DeployerLogEvents.ARTIFACT_NOT_FOUND, type, name, versionRange, this.repository.getName());
104: throw new DeploymentException("The URI '" + uri + "' references a non-existent artifact");
105: }
106: return artifactDescriptor;
107: } catch (IllegalArgumentException iae) {
108: this.eventLogger.log(DeployerLogEvents.REPOSITORY_DEPLOYMENT_INVALID_VERSION, versionString, uri);
109: throw new DeploymentException("The version '" + versionString + "' is invalid");
110: }
111: }
112: }