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 019 package org.apache.commons.modeler.ant; 020 021 022 import java.io.File; 023 import java.io.FileOutputStream; 024 import java.io.ObjectOutputStream; 025 import java.net.URL; 026 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 import org.apache.commons.modeler.ManagedBean; 030 import org.apache.commons.modeler.Registry; 031 import org.apache.tools.ant.BuildException; 032 033 /** 034 * Load descriptors into registry. 035 * 036 * @author Costin Manolache 037 */ 038 public final class RegistryTask { 039 private static Log log = LogFactory.getLog(RegistryTask.class); 040 041 public RegistryTask() { 042 } 043 044 String resource; 045 String file; 046 String type="MbeansDescriptorsDOMSource"; 047 048 /** Set the resource type that will be loaded 049 * 050 * @param type 051 */ 052 public void setType( String type ) { 053 this.type=type; 054 } 055 056 public void setFile( String file ) { 057 this.file=file; 058 } 059 060 public void setResource( String res ) { 061 this.resource=res; 062 } 063 064 String outFile; 065 066 public void setOut( String outFile ) { 067 this.outFile=outFile; 068 } 069 070 public void execute() throws Exception { 071 URL url=null; 072 073 if( resource != null ) { 074 url=this.getClass().getClassLoader().getResource(resource); 075 } else if( file != null ) { 076 File f=new File(file); 077 url=new URL("file", null, f.getAbsolutePath()); 078 } else { 079 throw new BuildException( "Resource or file attribute required"); 080 } 081 082 Registry.getRegistry().loadDescriptors( type, url, null); 083 084 if( outFile !=null ) { 085 FileOutputStream fos=new FileOutputStream(outFile); 086 ObjectOutputStream oos=new ObjectOutputStream(fos); 087 Registry reg=Registry.getRegistry(); 088 String beans[]=reg.findManagedBeans(); 089 ManagedBean mbeans[]=new ManagedBean[beans.length]; 090 for( int i=0; i<beans.length; i++ ) { 091 mbeans[i]=reg.findManagedBean(beans[i]); 092 } 093 oos.writeObject( mbeans ); 094 oos.flush(); 095 oos.close(); 096 fos.close(); 097 } 098 } 099 } 100