1 17 18 package org.apache.avalon.fortress.tools; 19 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 35 final class Service 36 { 37 private final Set m_components; 38 39 private final String m_type; 40 41 46 public Service( final String type ) 47 { 48 if ( type == null ) throw new NullPointerException ( "type" ); 49 50 m_type = type; 51 m_components = new HashSet (); 52 } 53 54 59 public String getType() 60 { 61 return m_type; 62 } 63 64 69 public void addComponent( final Component type ) 70 { 71 if ( type == null ) throw new NullPointerException ( "type" ); 72 73 m_components.add( type ); 74 } 75 76 public Iterator getComponents() 77 { 78 return m_components.iterator(); 79 } 80 81 87 public void serialize( final File rootDir ) throws IOException 88 { 89 if ( m_components.isEmpty() ) return; 90 91 final File serviceFile = new File ( rootDir, "META-INF/services/" + getType() ); 92 PrintWriter writer = null; 93 94 try 95 { 96 writer = new PrintWriter ( new FileWriter ( serviceFile ) ); 97 98 final Iterator it = m_components.iterator(); 99 while ( it.hasNext() ) 100 { 101 final Component comp = (Component) it.next(); 102 writer.println( comp.getType() ); 103 } 104 } 105 finally 106 { 107 if ( null != writer ) 108 { 109 writer.close(); 110 } 111 } 112 } 113 } 114 | Popular Tags |