| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds 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.jclouds.byon.suppliers; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.net.URI; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | |
| 29 | import org.jclouds.location.Provider; |
| 30 | import org.jclouds.logging.Logger; |
| 31 | import org.jclouds.util.Strings2; |
| 32 | |
| 33 | import com.google.common.annotations.VisibleForTesting; |
| 34 | import com.google.common.base.Function; |
| 35 | import com.google.common.base.Supplier; |
| 36 | import com.google.common.base.Throwables; |
| 37 | import com.google.inject.Inject; |
| 38 | import com.google.inject.name.Named; |
| 39 | |
| 40 | /** |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | public class SupplyFromProviderURIOrNodesProperty implements Supplier<InputStream>, Function<URI, InputStream> { |
| 44 | @Resource |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | private final URI url; |
| 47 | |
| 48 | @Inject(optional = true) |
| 49 | @Named("byon.nodes") |
| 50 | @VisibleForTesting |
| 51 | String nodes; |
| 52 | |
| 53 | @Inject |
| 54 | public SupplyFromProviderURIOrNodesProperty(@Provider URI url) { |
| 55 | this.url = checkNotNull(url, "url"); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public InputStream get() { |
| 60 | if (nodes != null) |
| 61 | return Strings2.toInputStream(nodes); |
| 62 | return apply(url); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return "[url=" + url + "]"; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public InputStream apply(URI input) { |
| 72 | try { |
| 73 | if (input.getScheme() != null && input.getScheme().equals("classpath")) |
| 74 | return getClass().getResourceAsStream(input.getPath()); |
| 75 | return input.toURL().openStream(); |
| 76 | } catch (IOException e) { |
| 77 | logger.error(e, "URI could not be read: %s", url); |
| 78 | Throwables.propagate(e); |
| 79 | return null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |