001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.xmlet;
009:
010: import java.util.Set;
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.io.File;
014: import java.io.IOException;
015: import java.io.FilenameFilter;
016: import javax.management.ObjectName;
017: import javax.management.InstanceNotFoundException;
018: import javax.management.MBeanServerInvocationHandler;
019: import javax.management.loading.MLetMBean;
020:
021: import org.huihoo.jfox.service.ServiceSupport;
022:
023: /**
024: * A extention MLet, it can monitor the folder, when add/remove a xmlet config
025: * file, it will register/unregister the MBean auto
026: *
027: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
028: */
029:
030: public class DirMLet extends ServiceSupport implements DirMLetMBean,
031: Runnable {
032:
033: /**
034: * the path which the Monitor will monitor
035: */
036: // private File path = new File("./");
037: private ObjectName mletObjectName = null;
038:
039: /**
040: * loaded pathes
041: */
042: private Set pathes = new HashSet();
043:
044: /**
045: * the path want to be load
046: */
047: private Set queryPathes = new HashSet();
048:
049: private long sleepTime = 5000L;
050:
051: public DirMLet() {
052:
053: }
054:
055: public DirMLet(String name) {
056: super (name);
057: }
058:
059: /**
060: * get loaded pathes
061: */
062: public String[] getPathes() {
063: return (String[]) pathes.toArray(new String[0]);
064: }
065:
066: public void addPath(String path) {
067: if (path.startsWith("./") && mletObjectName != null) { // relative path
068: path = getMLetInstance().getLibraryDirectory() + "/" + path;
069: }
070: File file = new File(path);
071: if (!file.isDirectory()) {
072: throw new RuntimeException("path must be a directory");
073: }
074: try {
075: synchronized (queryPathes) {
076: queryPathes.add(file.getCanonicalFile());
077: }
078: } catch (IOException e) {
079: e.printStackTrace();
080: logger.warn(e.getMessage());
081: }
082: }
083:
084: public void setMLet(ObjectName mletObjectName) {
085: this .mletObjectName = mletObjectName;
086: }
087:
088: public ObjectName getMLet() {
089: return mletObjectName;
090: }
091:
092: public MLetMBean getMLetInstance() {
093: if (mletObjectName == null)
094: return null;
095: try {
096: return (MLetMBean) MBeanServerInvocationHandler
097: .newProxyInstance(server, mletObjectName,
098: MLetMBean.class, false);
099: } catch (Exception e) {
100: logger.warn(e.getMessage(), e);
101: return null;
102: }
103: }
104:
105: /**
106: * set the sleep time in millisecond
107: */
108: public void setSleepTime(long milSec) {
109: sleepTime = milSec;
110: }
111:
112: public long getSleepTime() {
113: return sleepTime;
114: }
115:
116: protected void doInit() throws Exception {
117: if (mletObjectName == null) {
118: logger.warn("MLet not set yet, can not initialze DirMLet");
119: return;
120: }
121:
122: try {
123: if (!server.isInstanceOf(mletObjectName, MLetMBean.class
124: .getName())) {
125: logger.warn("the ObjectName " + mletObjectName
126: + " is not a " + MLetMBean.class.getName());
127: }
128: } catch (InstanceNotFoundException e) {
129: logger.warn("the ObjectName " + mletObjectName
130: + " is not registered", e);
131: }
132: }
133:
134: protected void doStart() throws Exception {
135:
136: new Thread(this ).start();
137: }
138:
139: protected void doStop() throws Exception {
140:
141: }
142:
143: protected void doDestroy() throws Exception {
144: mletObjectName = null;
145: }
146:
147: public void run() {
148: while (isRunning() && (mletObjectName != null)) {
149: synchronized (queryPathes) {
150: // loop the directory
151: for (Iterator iter = queryPathes.iterator(); iter
152: .hasNext();) {
153: File dir = (File) iter.next();
154: if (!pathes.contains(dir.toString())) {
155: getMBeansFromDirectory(dir);
156: }
157: pathes.add(dir.toString());
158: }
159: queryPathes.clear();
160: try {
161: Thread.sleep(sleepTime);
162: } catch (InterruptedException e) {
163: e.printStackTrace();
164: }
165: }
166: }
167: }
168:
169: private void getMBeansFromDirectory(File dir) {
170: if (dir.isFile())
171: return;
172: logger.info(dir.toString());
173: MLetMBean mlet = getMLetInstance();
174: File[] files = dir.listFiles(new FilenameFilter() {
175: public boolean accept(File dir, String name) {
176: if (name.endsWith(".xml")) {
177: return true;
178: } else {
179: return false;
180: }
181: }
182: });
183: for (int i = 0; i < files.length; i++) {
184: // logger.info(files[i].toString());
185: try {
186: mlet.getMBeansFromURL(files[i].toURL());
187: } catch (Exception e) {
188: e.printStackTrace();
189: logger.warn(e.getMessage());
190: }
191: }
192: }
193:
194: }
|