01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.commons.modeler.ant;
19:
20: import java.io.File;
21: import java.io.FileOutputStream;
22: import java.io.ObjectOutputStream;
23: import java.net.URL;
24:
25: import org.apache.commons.logging.Log;
26: import org.apache.commons.logging.LogFactory;
27: import org.apache.commons.modeler.ManagedBean;
28: import org.apache.commons.modeler.Registry;
29: import org.apache.tools.ant.BuildException;
30:
31: /**
32: * Load descriptors into registry.
33: *
34: * @author Costin Manolache
35: */
36: public final class RegistryTask {
37: private static Log log = LogFactory.getLog(RegistryTask.class);
38:
39: public RegistryTask() {
40: }
41:
42: String resource;
43: String file;
44: String type = "MbeansDescriptorsDOMSource";
45:
46: /** Set the resource type that will be loaded
47: *
48: * @param type
49: */
50: public void setType(String type) {
51: this .type = type;
52: }
53:
54: public void setFile(String file) {
55: this .file = file;
56: }
57:
58: public void setResource(String res) {
59: this .resource = res;
60: }
61:
62: String outFile;
63:
64: public void setOut(String outFile) {
65: this .outFile = outFile;
66: }
67:
68: public void execute() throws Exception {
69: URL url = null;
70:
71: if (resource != null) {
72: url = this .getClass().getClassLoader()
73: .getResource(resource);
74: } else if (file != null) {
75: File f = new File(file);
76: url = new URL("file", null, f.getAbsolutePath());
77: } else {
78: throw new BuildException(
79: "Resource or file attribute required");
80: }
81:
82: Registry.getRegistry().loadDescriptors(type, url, null);
83:
84: if (outFile != null) {
85: FileOutputStream fos = new FileOutputStream(outFile);
86: ObjectOutputStream oos = new ObjectOutputStream(fos);
87: Registry reg = Registry.getRegistry();
88: String beans[] = reg.findManagedBeans();
89: ManagedBean mbeans[] = new ManagedBean[beans.length];
90: for (int i = 0; i < beans.length; i++) {
91: mbeans[i] = reg.findManagedBean(beans[i]);
92: }
93: oos.writeObject(mbeans);
94: oos.flush();
95: oos.close();
96: fos.close();
97: }
98: }
99: }
|