1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.openjpa.lib.meta;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25 import java.net.URLDecoder;
26 import java.security.AccessController;
27 import java.security.PrivilegedActionException;
28
29 import org.apache.openjpa.lib.util.J2DoPrivHelper;
30
31 /**
32 * Iterator over the metadata resource represented by a URL.
33 *
34 * @author Abe White
35 * @nojavadoc
36 */
37 public class URLMetaDataIterator implements MetaDataIterator {
38
39 private final URL _url;
40 private boolean _iterated = false;
41
42 /**
43 * Constructor; supply resource URL.
44 */
45 public URLMetaDataIterator(URL url) {
46 _url = url;
47 }
48
49 public boolean hasNext() {
50 return _url != null && !_iterated;
51 }
52
53 public Object next() throws IOException {
54 if (!hasNext())
55 throw new IllegalStateException();
56
57 _iterated = true;
58 return _url;
59 }
60
61 public InputStream getInputStream() throws IOException {
62 if (!_iterated)
63 throw new IllegalStateException();
64 if (_url == null)
65 return null;
66 try {
67 return (InputStream) AccessController.doPrivileged(
68 J2DoPrivHelper.openStreamAction(_url));
69 } catch (PrivilegedActionException pae) {
70 throw (IOException) pae.getException();
71 }
72 }
73
74 public File getFile() {
75 if (!_iterated)
76 throw new IllegalStateException();
77 if (_url == null)
78 return null;
79 File file = new File(URLDecoder.decode(_url.getPath()));
80 return (((Boolean) AccessController.doPrivileged(
81 J2DoPrivHelper.existsAction(file))).booleanValue()) ? file:null;
82 }
83
84 public void close() {
85 }
86 }
87