KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > NetFileSrv


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.builders;
11
12 import java.util.*;
13
14 import org.mmbase.module.builders.vwms.*;
15 import org.mmbase.module.core.*;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * The NetFileServ builder contains information on services available to NetFile objects.
20  * It contains a list of possible service/subservice tasks. These tasks are then attached
21  * to entries in the VWMs builder, so it is possible to search for a VWM to handle a
22  * service/subservice request.
23  * The fields of NetFileServ are:<br />
24  * <ul>
25  * <li><code>service</code> : the main 'service' to be performed.
26  * Together with subservice, this determines the VWM that handles the transfer,
27  * i.e. 'pages/main' is handled by the {@link PageMaster} VWM.</li>
28  * <li><code>subservice</code> : the subservice to perform. i.e. in PageMaster, 'main' determines mirror sites and
29  * schedules tasks for mirroring (by creating net netfile entries), while 'mirror'
30  * performs the actual transfer to a mirror<br />
31  * Often one VWM handles multiple subservices, but this is not a given.</li>
32  * <li><code>options</code> : Currently unused (?)</li>
33  *</ul>
34  *
35  * @author Daniel Ockeloen
36  * @version $Id: NetFileSrv.java,v 1.13 2003/03/10 11:50:20 pierre Exp $
37  */

38 public class NetFileSrv extends MMObjectBuilder {
39
40     // Logger class
41
private static Logger log = Logging.getLoggerInstance(NetFileSrv.class.getName());
42
43     /**
44     * Cache of VWMS as they are related to a service.
45     */

46     Hashtable service2bot=new Hashtable();
47
48     /**
49      * What should a GUI display for this node.
50      * Returns a description of the service and subservice.
51      * @param node The node to display
52      * @return the display of the node as a <code>String</code>
53      */

54     public String JavaDoc getGUIIndicator(MMObjectNode node) {
55         String JavaDoc str=node.getStringValue("service");
56         str+="/"+node.getStringValue("subservice");
57         if (str.length()>15) {
58             return str.substring(0,12)+"...";
59         } else {
60             return str;
61         }
62     }
63
64     /**
65      * Handles a change of a netfiles node.
66      * Depending on the service/subservice of that node, a VWM (such as {@link PageMaster}) is invoked.
67      * This method is called from the Netfiles builder whenever a local or remote node change occurs.
68      * @param number Number of the node in the netfiles buidler than contain service request information.
69      * @param ctype the type of change on that node ("c" : node was changed)
70      * @return <code>true</code>
71      */

72     public boolean fileChange(String JavaDoc number,String JavaDoc ctype) {
73         try {
74             log.debug(number+" "+ctype);
75             MMObjectNode node=getNode(number);
76             String JavaDoc service=node.getStringValue("service");
77             String JavaDoc subservice=node.getStringValue("subservice");
78             String JavaDoc servicestr=service+"/"+subservice;
79             Object JavaDoc bot=service2bot.get(servicestr);
80             if (bot!=null) {
81                 if (bot instanceof VwmServiceInterface){
82                      ((VwmServiceInterface)bot).fileChange(number,ctype);
83                 } else {
84                     log.warn("Bot for "+servicestr+" is not a VwmServiceInterface.");
85                 }
86             } else {
87                 // figure out the bot for this service/subservice
88
bot=getAttachedBot(service,subservice);
89                 if (bot!=null) {
90                     service2bot.put(servicestr,bot);
91                     if (bot instanceof VwmServiceInterface) {
92                         ((VwmServiceInterface)bot).fileChange(number,ctype);
93                     } else {
94                         log.warn("Bot for "+servicestr+" is not a VwmServiceInterface.");
95                     }
96                 }
97             }
98         } catch (Exception JavaDoc e) {
99             log.error("FileChange Exception : "+e);
100             log.error(Logging.stackTrace(e));
101         }
102         return true;
103     }
104
105
106     /**
107      * Handles a service request on a file.
108      * Depending on the service/subservice, a VWM (such as {@link PageMaster}) is invoked.
109      * This method is called from the {@link org.mmbase.module.scancache} module to handle page caching.
110      * The VWM invoked is responsible for creating NetFile entries (if needed).
111      * @param service the service to be performed
112      * @param subservice the subservice to be performed
113      * @param filename the filename to service
114      * @return <code>true</code> if maintenance was performed, <code>false</code> otherwise
115      */

116     public boolean fileChange(String JavaDoc service,String JavaDoc subservice,String JavaDoc filename) {
117         String JavaDoc servicestr=service+"/"+subservice;
118         Object JavaDoc bot=service2bot.get(servicestr);
119         if (bot!=null) {
120             if (bot instanceof VwmServiceInterface) ((VwmServiceInterface)bot).fileChange(service,subservice,filename);
121         } else {
122             // figure out the bot for this service/subservice
123
bot=getAttachedBot(service,subservice);
124             if (bot!=null) {
125                 service2bot.put(servicestr,bot);
126                 if (bot instanceof VwmServiceInterface) ((VwmServiceInterface)bot).fileChange(service,subservice,filename);
127             }
128         }
129         return true;
130     }
131
132     /**
133      * Retrieve a vwm (a 'bot') for the service/subservice combination.
134      * This is achieved by following the relations of NetFileServ entries to entries in the VWMs builder.
135      * Note that while, theoretically, more vwms could be related, only one (the first) is returned.
136      * @param service the service to search for
137      * @param subservice the subservice to search for
138      * @return an object that implements VWMInterface if successful, a dummy object otherwise.
139      * This system is a bit odd as getAttachedBot would ideally have VwmServiceInterface as
140      * a return type. Possibly change this later, though this also means changing the caching system.
141      */

142     public Object JavaDoc getAttachedBot(String JavaDoc service,String JavaDoc subservice) {
143         Enumeration e=search("WHERE service='"+service+"' AND subservice='"+subservice+"'");
144
145         if(!e.hasMoreElements()) {
146             log.error("No entry with service="+service+" and subservice="+subservice+" found in netfilesrv");
147         }
148         while (e.hasMoreElements()) {
149             MMObjectNode node=(MMObjectNode)e.nextElement();
150             int number=node.getIntValue("number");
151             Enumeration f=mmb.getInsRel().getRelated(""+number,"vwms");
152
153             if(!f.hasMoreElements()) {
154                 log.error("No Vwms related to the service="+service+" and subservice="+subservice+" found in netfilesrv");
155             }
156             while (f.hasMoreElements()) {
157                 MMObjectNode vwmnode=(MMObjectNode)f.nextElement();
158                 Vwms vwms=(Vwms)mmb.getMMObject("vwms");
159                 if (vwms!=null) {
160                     String JavaDoc name=vwmnode.getStringValue("name");
161                     VwmServiceInterface vwm=(VwmServiceInterface)vwms.getVwm(name);
162                     if (vwm!=null) {
163                         return vwm;
164                     } else {
165                         log.error("Vwms "+name+" not loaded.");
166                     }
167                 } else {
168                     log.error("Builder vwms not loaded.");
169                 }
170             }
171         }
172         return new Object JavaDoc(); // needed to fill a Dummy in the cache.
173
}
174 }
175
Popular Tags