1 /* 2 * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.util.lang.classpath; 17 18 import java.util.Iterator; 19 import java.util.ServiceConfigurationError; 20 import java.util.ServiceLoader; 21 import java.util.logging.Logger; 22 23 /** 24 * A factory to create instances of {@link ClassPathListing}. 25 */ 26 public final class ClassPathListingFactory 27 { 28 // ********************************* Fields ********************************* 29 30 // --- constants ------------------------------------------------------------ 31 32 /** 33 * Reference to the logger for this class. 34 */ 35 private static final Logger LOG = Logger 36 .getLogger(ClassPathListingFactory.class.getName()); 37 38 // --- members -------------------------------------------------------------- 39 40 // ****************************** Initializer ******************************* 41 42 // ****************************** Constructors ****************************** 43 44 // ****************************** Inner Classes ***************************** 45 46 // ********************************* Methods ******************************** 47 48 // --- init ----------------------------------------------------------------- 49 50 // --- get&set -------------------------------------------------------------- 51 52 // --- business ------------------------------------------------------------- 53 54 /** 55 * Creates an instance of {@link ClassPathListing} using the configuration of 56 * the service in <code>META-INF/services</code>. 57 * 58 * @return instance of {@link ClassPathListing}. 59 */ 60 public ClassPathListing create() 61 { 62 final Iterator<ClassPathListing> iterator = 63 ServiceLoader.load(ClassPathListing.class).iterator(); 64 65 final StringBuilder buffer = new StringBuilder(64); 66 ClassPathListing instance = null; 67 while (iterator.hasNext()) 68 { 69 try 70 { 71 if (instance != null) 72 { 73 final String implementation = iterator.next().getClass().getName(); 74 buffer.append("\nDuplicated implementation rejected: " 75 + implementation); 76 continue; 77 } 78 79 instance = iterator.next(); 80 } 81 catch (final ServiceConfigurationError e) 82 { 83 buffer.append("\nError encountered: ").append(e.getMessage()); 84 } 85 } 86 87 if (buffer.length() > 0) 88 { 89 LOG.warning("Problems encountered while fetching implementations of '" 90 + ClassPathListing.class.getName() + "':" + buffer); 91 } 92 93 if (instance == null) 94 { 95 throw new IllegalStateException( 96 "Cannot create instance of implementation of '" 97 + ClassPathListing.class.getName() + "'."); 98 } 99 100 return instance; 101 } 102 103 // --- object basics -------------------------------------------------------- 104 105 }