KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enhydraManager > business > GlobalImpl


1
2 package enhydraManager.business;
3
4
5 import com.lutris.appserver.server.*;
6
7 import com.lutris.appserver.server.httpPresentation.servlet.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
10 import java.net.URL JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import org.apache.catalina.core.*;
13 import org.apache.catalina.*;
14 import org.apache.catalina.core.StandardContext;
15 import enhydraManager.spec.*;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Vector JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import org.apache.commons.modeler.Registry;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25
26
27 public class GlobalImpl implements Global {
28
29
30
31 private static Hashtable JavaDoc udeployed = new Hashtable JavaDoc();
32
33
34 public void initialize() {
35   
36 }
37
38
39 public static String JavaDoc[] getServices()
40 {
41      Server JavaDoc server = ServerFactory.getServer();
42      Service[] service = server.findServices();
43
44    String JavaDoc result[] = new String JavaDoc[service.length];
45    
46    for(int i=0;i<service.length;i++)
47     {
48    result[i]=service[i].getName();
49      
50      }
51   
52    
53    return result;
54 }
55
56
57
58 public static Vector JavaDoc getHostNames(String JavaDoc service){
59   
60    Vector JavaDoc result = new Vector JavaDoc();
61
62       Server JavaDoc server = ServerFactory.getServer();
63       Service[] services = server.findServices();
64   
65    for(int i=0;i<services.length;i++)
66     {
67     
68      if(services[i].getName().equals(service))
69       {
70
71         Container cont= services[i].getContainer();
72            
73         Container children[] = ((StandardEngine)cont).findChildren();
74
75
76          for (int k = 0; k < children.length; k++)
77           {
78            StandardHost host=new StandardHost();
79   
80            if (children[k] instanceof Host)
81             {
82              host= (StandardHost)children[k];
83              String JavaDoc hostName=((StandardHost)host).getName();
84              result.add(hostName);
85             }
86     
87           }
88         }
89      }
90    return result;
91 }
92
93
94
95 public static Vector JavaDoc[] getServletNames(String JavaDoc host,String JavaDoc service)
96  {
97  
98     Vector JavaDoc[] result = new Vector JavaDoc[3];
99        result[0] = new Vector JavaDoc();
100        result[1] = new Vector JavaDoc();
101        result[2] = new Vector JavaDoc();
102
103       Server JavaDoc server = ServerFactory.getServer();
104       Service[] services = server.findServices();
105   
106    for(int i=0;i<services.length;i++)
107     {
108     
109      if(services[i].getName().equals(service))
110       {
111
112         Container cont= services[i].getContainer();
113            
114         Container children[] = ((StandardEngine)cont).findChildren();
115
116
117          for (int j = 0; j < children.length; j++)
118           {
119            StandardHost tempHost=new StandardHost();
120   
121            if (children[j] instanceof Host)
122             {
123             
124              if (children[j].getName().equals(host))
125               {
126                tempHost= (StandardHost)children[j];
127               Container childrenApp[] = ((StandardHost)tempHost).findChildren();
128                  
129                  for (int k = 0; k < childrenApp.length; k++)
130                   {
131                    result[0].add(((Context)childrenApp[k]).getName());
132                    result[1].add("Deployed");
133                    boolean started=((StandardContext)childrenApp[k]).getAvailable();
134                    if(started)
135                     result[2].add("Started");
136                    else
137                     result[2].add("Stoped");
138                   }
139               }
140             }
141           }
142        }
143      }
144  
145       Hashtable JavaDoc hosts = (Hashtable JavaDoc)udeployed.get(service);
146       
147        if(hosts!=null)
148         {
149      
150          Vector JavaDoc applications = (Vector JavaDoc)hosts.get(host);
151         
152           if(applications!=null)
153            {
154              for(int v=0;v<applications.size();v++)
155                {
156                 result[0].add((String JavaDoc)applications.elementAt(v) );
157                 result[1].add("UnDeployed");
158                 result[2].add("Stoped");
159                }
160            }
161         }
162    return result;
163  }
164  
165
166 public static StandardContext getServlet(String JavaDoc service,String JavaDoc host,String JavaDoc name)
167  {
168     
169     if(name.equals("/"))
170       name = "";
171     Server JavaDoc server = ServerFactory.getServer();
172       Service[] services = server.findServices();
173   
174    for(int i=0;i<services.length;i++)
175     {
176     
177      if(services[i].getName().equals(service))
178       {
179
180   
181         Container cont= services[i].getContainer();
182            
183         Container children[] = ((StandardEngine)cont).findChildren();
184
185
186          for (int j = 0; j < children.length; j++)
187           {
188            StandardHost tempHost=new StandardHost();
189   
190            if (children[j] instanceof Host)
191             {
192             
193              if (children[j].getName().equals(host))
194               {
195                tempHost= (StandardHost)children[j];
196              Container childrenApp[] = ((StandardHost)tempHost).findChildren();
197                  
198                  for (int k = 0; k < childrenApp.length; k++)
199                   {
200                   if(((Context)childrenApp[k]).getName().equals(name))
201                     return (StandardContext)childrenApp[k];
202                   }
203               }
204             
205             }
206           }
207        }
208      }
209    
210    return null;
211  }
212
213
214 public static String JavaDoc manage(String JavaDoc service,String JavaDoc host,String JavaDoc application,String JavaDoc action)throws Exception JavaDoc
215   {
216
217
218  
219 if(action.equals("delete"))
220 {
221    return delete(service,host,application);
222    
223 }
224
225     Server JavaDoc server = ServerFactory.getServer();
226       
227     Service[] services = server.findServices();
228     
229     StandardHost h=new StandardHost();
230     
231     
232    for(int z=0;z<services.length;z++)
233     {
234     
235     if(services[z].getName().equals(service))
236      {
237       Container cont= services[z].getContainer();
238       Container children[] = ((StandardEngine)cont).findChildren();
239
240      
241        for (int i = 0; i < children.length; i++)
242         {
243   
244             StandardHost tmp=new StandardHost();
245       
246             if (children[i] instanceof Host)
247               tmp= (StandardHost)children[i];
248             
249            String JavaDoc hostName=((StandardHost)tmp).getName();
250            if(hostName.equals(host))
251             {
252               h=(StandardHost)tmp;
253              break;
254             }
255          }
256         }
257    }
258    
259  if(h==null)
260   return " Could not find host "+host;
261  
262   
263   application="/"+application;
264
265  if(application.equals("/"))
266       application = "";
267    
268 if(action.equals("deploy"))
269 {
270     
271     try{
272         MBeanServer JavaDoc mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
273         String JavaDoc[] params = { application };
274         String JavaDoc[] signature = { "java.lang.String" };
275         Engine engine = (Engine) h.getParent();
276         
277         ObjectName JavaDoc oname = new ObjectName JavaDoc(engine.getName()
278                         + ":type=Deployer,host=" + h.getName());
279       
280          mBeanServer.invoke(oname, "addServiced", params, signature);
281          mBeanServer.invoke(oname, "check", params, signature);
282          mBeanServer.invoke(oname, "removeServiced", params, signature);
283    
284    
285     Hashtable JavaDoc hosts = (Hashtable JavaDoc)udeployed.get(service);
286       
287        if(hosts!=null)
288         {
289           Vector JavaDoc applications = (Vector JavaDoc)hosts.get(host);
290         
291           if(applications!=null)
292            {
293            if(applications.contains(application))
294              applications.remove(application);
295            }
296         }
297    
298     
299    }catch(IllegalArgumentException JavaDoc one){
300       System.out.println(one);
301        return "Application "+application+" didn`t deployed";
302    }catch(IllegalStateException JavaDoc two){
303       System.out.println(two);
304       return "Application "+application+" didn`t deployed";
305    }
306      return "Application "+application+" succesfuly Deployed";
307
308 }
309 else if(action.equals("undeploy"))
310 {
311   MBeanServer JavaDoc mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
312       
313         String JavaDoc[] params = { application };
314         String JavaDoc[] signature = { "java.lang.String" };
315         Engine engine = (Engine) h.getParent();
316      
317   
318
319         ObjectName JavaDoc oname = new ObjectName JavaDoc(engine.getName()
320                         + ":type=Deployer,host=" + h.getName());
321     
322      
323        mBeanServer.invoke(oname, "addServiced", params, signature);
324        mBeanServer.invoke(oname, "unmanageApp", params, signature);
325        
326         try {
327               Context context = (Context) h.findChild(application);
328               if (context != null) {
329                     ((Lifecycle) context).stop();
330                }
331                   
332                 } catch (Throwable JavaDoc t) {
333                   }
334        
335        mBeanServer.invoke(oname, "removeServiced", params, signature);
336       
337       Hashtable JavaDoc hosts = (Hashtable JavaDoc)udeployed.get(service);
338       
339        if(hosts!=null)
340         {
341           Vector JavaDoc applications = (Vector JavaDoc)hosts.get(host);
342         
343           if(applications!=null)
344            {
345            if(!applications.contains(application))
346            {
347              applications.add(application);
348      
349            }
350            
351            }
352           else
353            {
354            Vector JavaDoc temp = new Vector JavaDoc();
355            temp.add(application);
356            hosts.put(host,temp);
357    
358            }
359         
360         }
361         else
362         {
363         
364          Hashtable JavaDoc hostss = new Hashtable JavaDoc();
365          Vector JavaDoc ap = new Vector JavaDoc();
366          ap.add(application);
367          hostss.put(host,ap);
368          udeployed.put(service,hostss);
369   
370         }
371
372    return "Application "+application+" succesfuly UnDeployed";
373
374 }
375 else if(action.equals("start"))
376 {
377 Context context = (Context) h.findChild(application);
378        if (context == null) {
379                 return "Could not start application "+application+" , application isn`t deployed ";
380           }
381       ((Lifecycle) context).start();
382        return "Application "+application+" succesfuly Started";
383
384 }
385 else if(action.equals("stop"))
386 {
387    
388     Context context = (Context)h.findChild(application);
389    
390          if (context == null) {
391                return "Could not stop application "+application+" , application isn`t deployed ";
392             }
393             ((StandardContext) context).stop();
394          return "Application "+application+" succesfuly Stoped";
395   }
396 else if(action.equals("reload"))
397 {
398   
399     Context context = (Context)h.findChild(application);
400    
401          if (context == null) {
402                       return "Could not reload application "+application+" , application isn`t deployed ";
403      
404             }
405             ((StandardContext) context).stop();
406             ((Lifecycle) context).start();
407         return "Application "+application+" succesfuly Reloaded";
408   }
409
410 return "Action "+action+" isn`t supported";
411   
412  }
413
414    protected static String JavaDoc delete(String JavaDoc service,String JavaDoc host,String JavaDoc application) {
415        
416        
417        if(!application.equals(""))
418         application="/"+application;
419         
420        boolean undeployed = false;
421        
422        
423        Hashtable JavaDoc hosts = (Hashtable JavaDoc)udeployed.get(service);
424        if(hosts!=null)
425         {
426          Vector JavaDoc applications = (Vector JavaDoc)hosts.get(host);
427           if(applications!=null)
428            {
429                    if(applications.contains(application))
430                     {
431                       undeployed =true;
432                       applications.remove(application);
433                     }
434            }
435         }
436         
437      
438         try {
439               Server JavaDoc server = ServerFactory.getServer();
440       
441                   Service[] services = server.findServices();
442     
443                   
444                   StandardHost h = null;
445                   Engine engine = null;
446                 
447                   
448                    
449               for(int z=0;z<services.length;z++)
450                 {
451                     Container cont= services[z].getContainer();
452                   
453                 if(services[z].getName().equals(service))
454                   {
455                 
456                   engine = (StandardEngine)cont;
457                   
458                      Container children[] = ((StandardEngine)cont).findChildren();
459      
460                      for (int i = 0; i < children.length; i++)
461                       {
462   
463                         StandardHost tmp=new StandardHost();
464       
465                       if (children[i] instanceof Host)
466                         tmp= (StandardHost)children[i];
467             
468                         String JavaDoc hostName=((StandardHost)tmp).getName();
469                        if(hostName.equals(host))
470                          {
471                            h=(StandardHost)tmp;
472                           break;
473                          }
474                        }
475                     }
476                  }
477             String JavaDoc appBase = null;
478             File JavaDoc appBaseDir = null;
479             appBase = ((Host)h).getAppBase();
480             appBaseDir = new File JavaDoc(appBase);
481                 if (!appBaseDir.isAbsolute()) {
482                     appBaseDir = new File JavaDoc(System.getProperty("catalina.base"),
483                                           appBase);
484                 }
485          if(!undeployed)
486            {
487      
488            MBeanServer JavaDoc mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
489       
490           
491              String JavaDoc[] params = { application };
492              String JavaDoc[] signature = { "java.lang.String" };
493         
494              ObjectName JavaDoc oname = new ObjectName JavaDoc(engine.getName()
495                         + ":type=Deployer,host=" + h.getName());
496       
497              mBeanServer.invoke(oname, "addServiced", params, signature);
498              mBeanServer.invoke(oname, "unmanageApp", params, signature);
499          
500               Context context = (Context) h.findChild(application);
501               if (context != null) {
502                     ((Lifecycle) context).stop();
503                }
504          
505              mBeanServer.invoke(oname, "removeServiced", params, signature);
506             }
507             
508             if(application.equals(""))
509               application="/ROOT";
510                
511                     File JavaDoc war = new File JavaDoc(appBaseDir, application + ".war");
512                     File JavaDoc dir = new File JavaDoc(appBaseDir, application);
513                     File JavaDoc configBase = null;
514                     
515                try {
516                  configBase = new File JavaDoc(System.getProperty("catalina.base"), "conf");
517                  configBase = new File JavaDoc(configBase, engine.getName());
518                  configBase = new File JavaDoc(configBase, h.getName());
519                 
520                  } catch (Throwable JavaDoc t) {
521                t.printStackTrace();
522                  }
523               
524                
525                File JavaDoc xml = new File JavaDoc(configBase, application + ".xml");
526                 
527                 
528                      
529                     if (war.exists())
530                         war.delete();
531                                      
532                     if (dir.exists())
533                         deleteDir(dir);
534                             
535                     if(xml.exists())
536                         xml.delete();
537                   
538           
539         } catch (Throwable JavaDoc t) {
540             t.printStackTrace();
541             return "Application "+application+" didn`t Deleted";
542            }
543        return "Application "+application+" succesfuly Deleted";
544     }
545     
546      protected static void deleteDir(File JavaDoc dir) {
547
548         String JavaDoc files[] = dir.list();
549         if (files == null) {
550             files = new String JavaDoc[0];
551         }
552         for (int i = 0; i < files.length; i++) {
553             File JavaDoc file = new File JavaDoc(dir, files[i]);
554             if (file.isDirectory()) {
555                 deleteDir(file);
556             } else {
557                 file.delete();
558             }
559         }
560         dir.delete();
561
562     }
563  
564  
565
566  public static String JavaDoc deploy(String JavaDoc service, String JavaDoc host,String JavaDoc contextPath,String JavaDoc contextFilePath, String JavaDoc war)
567   {
568     
569       Server JavaDoc server = ServerFactory.getServer();
570       
571                   Service[] services = server.findServices();
572     
573                   
574                   StandardHost h = null;
575                   Engine engine = null;
576                 
577                   
578                    
579               for(int z=0;z<services.length;z++)
580                 {
581                     Container cont= services[z].getContainer();
582                   
583                 if(services[z].getName().equals(service))
584                   {
585                 
586                   engine = (StandardEngine)cont;
587                   
588                      Container children[] = ((StandardEngine)cont).findChildren();
589      
590                      for (int i = 0; i < children.length; i++)
591                       {
592   
593                         StandardHost tmp=new StandardHost();
594       
595                       if (children[i] instanceof Host)
596                         tmp= (StandardHost)children[i];
597             
598                         String JavaDoc hostName=((StandardHost)tmp).getName();
599                        if(hostName.equals(host))
600                          {
601                            h=(StandardHost)tmp;
602                           break;
603                          }
604                        }
605                     }
606                  }
607                  
608     
609       File JavaDoc configBase = new File JavaDoc(System.getProperty("catalina.base"), "conf");
610           configBase = new File JavaDoc(configBase, engine.getName());
611           configBase = new File JavaDoc(configBase, h.getName());
612     
613         String JavaDoc appBase = null;
614             File JavaDoc appBaseDir = null;
615             appBase = ((Host)h).getAppBase();
616             appBaseDir = new File JavaDoc(appBase);
617                 if (!appBaseDir.isAbsolute()) {
618                     appBaseDir = new File JavaDoc(System.getProperty("catalina.base"),
619                                           appBase);
620                 }
621     
622     
623     
624     
625     if (contextFilePath != null && contextFilePath.length() == 0) {
626             contextFilePath = null;
627         }
628         if (war != null && war.length() == 0) {
629             war = null;
630         }
631           
632         if (contextPath == null || contextPath.length() == 0 || !contextPath.startsWith("/"))
633          {
634         
635            contextPath = null;
636         
637         if(war!=null||contextFilePath!=null)
638          {
639            
640            
641            if(war!=null&&war.endsWith(".war"))
642             {
643              contextPath = war.substring(0,war.indexOf(".war"));
644             }
645            else if(war!=null)
646             {
647              contextPath = new String JavaDoc(war);
648             }
649             
650            
651            if(contextPath==null&&contextFilePath.endsWith(".xml"))
652             {
653              contextPath = contextFilePath.substring(0,contextFilePath.indexOf(".xml"));
654             }
655             
656             if (contextPath != null)
657             {
658             try
659             {
660             contextPath = contextPath.substring(contextPath.lastIndexOf(File.separator),contextPath.length());
661             contextPath = contextPath.replaceAll("\\\\","/");
662             }catch(Exception JavaDoc w){
663                  return "Application didn`t deployed, problem resolving Context path of application";
664                 }
665             }
666             else
667             {
668              return "Application didn`t deployed, problem resolving Context path of application";
669             }
670          }
671         else
672         {
673           return "Application didn`t deployed, problem resolving Context path of application";
674         }
675        }
676        
677         
678         Context context = (Context) h.findChild(contextPath);
679      
680        if (context != null) {
681                        return "Application didn`t deployed , application already exsist !!!";
682         }
683         
684         
685         if (contextFilePath != null && (contextFilePath.startsWith("file:"))) {
686             contextFilePath = contextFilePath.substring("file:".length());
687         }
688         
689         if (war != null && (war.startsWith("file:"))) {
690             war = war.substring("file:".length());
691         }
692         
693         
694          try {
695                    
696                     if (contextFilePath != null) {
697                        if(!copy(new File JavaDoc(contextFilePath),new File JavaDoc(configBase, contextPath + ".xml")))
698                         return "Application didn`t deployed, problem while uploading context xml file : "+contextFilePath;
699                     }
700                     
701                     if (war != null) {
702                         if (war.endsWith(".war")) {
703                             if(!copy(new File JavaDoc(war),new File JavaDoc(appBaseDir, contextPath + ".war")))
704                             return "Application didn`t deployed, problem while uploading war "+war+" of application ";
705                         } else {
706                             if(!copy(new File JavaDoc(war),new File JavaDoc(appBaseDir, contextPath)))
707                            return "Application didn`t deployed , problem while uploading directory "+war+" of application ";
708                         }
709                     }
710                   
711                  
712         MBeanServer JavaDoc mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
713         String JavaDoc[] params = { contextPath };
714         String JavaDoc[] signature = { "java.lang.String" };
715         
716         ObjectName JavaDoc oname = new ObjectName JavaDoc(engine.getName()
717                         + ":type=Deployer,host=" + h.getName());
718       
719                 
720                  
721                  mBeanServer.invoke(oname, "addServiced", params, signature);
722                  mBeanServer.invoke(oname, "check", params, signature);
723                  mBeanServer.invoke(oname, "removeServiced", params, signature);
724                
725         
726             Context con = (Context) h.findChild(contextPath);
727             
728             if (con != null && con.getConfigured())
729             {
730                  return "Application "+contextPath+" succesfuly deployed ";
731             }
732             else if(con != null)
733             {
734                 return "Application"+contextPath+" didn`t deployed, appllication didn`t configured properly";
735             }
736             else
737             {
738              if(contextPath!=null)
739                return "Application "+contextPath+" didn`t deployed";
740              else
741                 return "Application didn`t deployed";
742             }
743         
744         
745         } catch (Throwable JavaDoc t) {
746          
747            if(contextPath!=null)
748                return "Application "+contextPath+" didn`t deployed";
749              else
750                 return "Application didn`t deployed";
751           
752  
753        }
754         
755         
756         
757   }
758  
759   
760    public static boolean copy(File JavaDoc src, File JavaDoc dest) {
761         return copyInternal(src, dest, new byte[4096]);
762     }
763
764     
765     public static boolean copyInternal(File JavaDoc src, File JavaDoc dest, byte[] buf) {
766         
767         boolean result = true;
768         
769         String JavaDoc files[] = null;
770         if (src.isDirectory()) {
771             files = src.list();
772             result = dest.mkdir();
773         } else {
774             files = new String JavaDoc[1];
775             files[0] = "";
776         }
777         if (files == null) {
778             files = new String JavaDoc[0];
779         }
780         for (int i = 0; (i < files.length) && result; i++) {
781             File JavaDoc fileSrc = new File JavaDoc(src, files[i]);
782             File JavaDoc fileDest = new File JavaDoc(dest, files[i]);
783             if (fileSrc.isDirectory()) {
784                 result = copyInternal(fileSrc, fileDest, buf);
785             } else {
786                 FileInputStream JavaDoc is = null;
787                 FileOutputStream JavaDoc os = null;
788                 try {
789                     is = new FileInputStream JavaDoc(fileSrc);
790                     os = new FileOutputStream JavaDoc(fileDest);
791                     int len = 0;
792                     while (true) {
793                         len = is.read(buf);
794                         if (len == -1)
795                             break;
796                         os.write(buf, 0, len);
797                     }
798                 } catch (IOException JavaDoc e) {
799                  // e.printStackTrace();
800
result = false;
801                 } finally {
802                     if (is != null) {
803                         try {
804                             is.close();
805                         } catch (IOException JavaDoc e) {
806                         }
807                     }
808                     if (os != null) {
809                         try {
810                             os.close();
811                         } catch (IOException JavaDoc e) {
812                         }
813                     }
814                 }
815             }
816         }
817         return result;
818         
819     }
820   
821     
822 }
Popular Tags