001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.repository.url;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.MalformedURLException;
023: import java.net.URL;
024:
025: import org.apache.ivy.plugins.repository.Resource;
026: import org.apache.ivy.util.url.URLHandlerRegistry;
027: import org.apache.ivy.util.url.URLHandler.URLInfo;
028:
029: public class URLResource implements Resource {
030: private URL _url;
031:
032: private boolean _init = false;
033:
034: private long _lastModified;
035:
036: private long _contentLength;
037:
038: private boolean _exists;
039:
040: public URLResource(URL url) {
041: _url = url;
042: }
043:
044: public String getName() {
045: return _url.toExternalForm();
046: }
047:
048: public Resource clone(String cloneName) {
049: try {
050: return new URLResource(new URL(cloneName));
051: } catch (MalformedURLException e) {
052: throw new IllegalArgumentException(
053: "bad clone name provided: not suitable for an URLResource: "
054: + cloneName);
055: }
056: }
057:
058: public long getLastModified() {
059: if (!_init) {
060: init();
061: }
062: return _lastModified;
063: }
064:
065: private void init() {
066: URLInfo info = URLHandlerRegistry.getDefault().getURLInfo(_url);
067: _contentLength = info.getContentLength();
068: _lastModified = info.getLastModified();
069: _exists = info.isReachable();
070: _init = true;
071: }
072:
073: public long getContentLength() {
074: if (!_init) {
075: init();
076: }
077: return _contentLength;
078: }
079:
080: public boolean exists() {
081: if (!_init) {
082: init();
083: }
084: return _exists;
085: }
086:
087: public URL getURL() {
088: return _url;
089: }
090:
091: public String toString() {
092: return getName();
093: }
094:
095: public boolean isLocal() {
096: return false;
097: }
098:
099: public InputStream openStream() throws IOException {
100: return _url.openStream();
101: }
102: }
|