1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.jetty.webapp;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.security.PermissionCollection;
22 import java.util.EventListener;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSessionActivationListener;
30 import javax.servlet.http.HttpSessionAttributeListener;
31 import javax.servlet.http.HttpSessionBindingListener;
32 import javax.servlet.http.HttpSessionListener;
33
34 import org.mortbay.jetty.Connector;
35 import org.mortbay.jetty.HandlerContainer;
36 import org.mortbay.jetty.Server;
37 import org.mortbay.jetty.deployer.ContextDeployer;
38 import org.mortbay.jetty.deployer.WebAppDeployer;
39 import org.mortbay.jetty.handler.ContextHandler;
40 import org.mortbay.jetty.handler.ContextHandlerCollection;
41 import org.mortbay.jetty.handler.ErrorHandler;
42 import org.mortbay.jetty.handler.HandlerCollection;
43 import org.mortbay.jetty.security.SecurityHandler;
44 import org.mortbay.jetty.servlet.Context;
45 import org.mortbay.jetty.servlet.ErrorPageErrorHandler;
46 import org.mortbay.jetty.servlet.ServletHandler;
47 import org.mortbay.jetty.servlet.SessionHandler;
48 import org.mortbay.log.Log;
49 import org.mortbay.resource.JarResource;
50 import org.mortbay.resource.Resource;
51 import org.mortbay.util.IO;
52 import org.mortbay.util.LazyList;
53 import org.mortbay.util.Loader;
54 import org.mortbay.util.StringUtil;
55 import org.mortbay.util.URIUtil;
56 import org.mortbay.util.UrlEncoded;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class WebAppContext extends Context
74 {
75 public final static String WEB_DEFAULTS_XML="org/mortbay/jetty/webapp/webdefault.xml";
76 public final static String ERROR_PAGE="org.mortbay.jetty.error_page";
77
78 private static String[] __dftConfigurationClasses =
79 {
80 "org.mortbay.jetty.webapp.WebInfConfiguration",
81 "org.mortbay.jetty.webapp.WebXmlConfiguration",
82 "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",
83 "org.mortbay.jetty.webapp.TagLibConfiguration"
84 } ;
85 private String[] _configurationClasses=__dftConfigurationClasses;
86 private Configuration[] _configurations;
87 private String _defaultsDescriptor=WEB_DEFAULTS_XML;
88 private String _descriptor=null;
89 private String _overrideDescriptor=null;
90 private boolean _distributable=false;
91 private boolean _extractWAR=true;
92 private boolean _copyDir=false;
93 private boolean _logUrlOnStart =false;
94 private boolean _parentLoaderPriority= Boolean.getBoolean("org.mortbay.jetty.webapp.parentLoaderPriority");
95 private PermissionCollection _permissions;
96 private String[] _systemClasses = {"java.","javax.servlet.","javax.xml.","org.mortbay.","org.xml.","org.w3c.", "org.apache.commons.logging.", "org.apache.log4j."};
97 private String[] _serverClasses = {"-org.mortbay.jetty.plus.jaas.", "org.mortbay.jetty.", "org.slf4j."};
98 private File _tmpDir;
99 private boolean _isExistingTmpDir;
100 private String _war;
101 private String _extraClasspath;
102 private Throwable _unavailableException;
103
104
105 private transient Map _resourceAliases;
106 private transient boolean _ownClassLoader=false;
107 private transient boolean _unavailable;
108
109 public static ContextHandler getCurrentWebAppContext()
110 {
111 ContextHandler.SContext context=ContextHandler.getCurrentContext();
112 if (context!=null)
113 {
114 ContextHandler handler = context.getContextHandler();
115 if (handler instanceof WebAppContext)
116 return (ContextHandler)handler;
117 }
118 return null;
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public static void addWebApplications(Server server,
139 String webapps,
140 String defaults,
141 boolean extract,
142 boolean java2CompliantClassLoader)
143 throws IOException
144 {
145 addWebApplications(server, webapps, defaults, __dftConfigurationClasses, extract, java2CompliantClassLoader);
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public static void addWebApplications(Server server,
169 String webapps,
170 String defaults,
171 String[] configurations,
172 boolean extract,
173 boolean java2CompliantClassLoader)
174 throws IOException
175 {
176 HandlerCollection contexts = (HandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class);
177 if (contexts==null)
178 contexts = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class);
179
180 addWebApplications(contexts,webapps,defaults,configurations,extract,java2CompliantClassLoader);
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public static void addWebApplications(HandlerContainer contexts,
204 String webapps,
205 String defaults,
206 boolean extract,
207 boolean java2CompliantClassLoader)
208 throws IOException
209 {
210 addWebApplications(contexts, webapps, defaults, __dftConfigurationClasses, extract, java2CompliantClassLoader);
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public static void addWebApplications(HandlerContainer contexts,
234 String webapps,
235 String defaults,
236 String[] configurations,
237 boolean extract,
238 boolean java2CompliantClassLoader)
239 throws IOException
240 {
241 Log.warn("Deprecated configuration used for "+webapps);
242 WebAppDeployer deployer = new WebAppDeployer();
243 deployer.setContexts(contexts);
244 deployer.setWebAppDir(webapps);
245 deployer.setConfigurationClasses(configurations);
246 deployer.setExtract(extract);
247 deployer.setParentLoaderPriority(java2CompliantClassLoader);
248 try
249 {
250 deployer.start();
251 }
252 catch(IOException e)
253 {
254 throw e;
255 }
256 catch(Exception e)
257 {
258 throw new RuntimeException(e);
259 }
260 }
261
262
263 public WebAppContext()
264 {
265 this(null,null,null,null);
266 }
267
268
269
270
271
272
273 public WebAppContext(String webApp,String contextPath)
274 {
275 super(null,contextPath,SESSIONS|SECURITY);
276 setContextPath(contextPath);
277 setWar(webApp);
278 setErrorHandler(new ErrorPageErrorHandler());
279 }
280
281
282
283
284
285
286
287 public WebAppContext(HandlerContainer parent, String webApp, String contextPath)
288 {
289 super(parent,contextPath,SESSIONS|SECURITY);
290 setWar(webApp);
291 setErrorHandler(new ErrorPageErrorHandler());
292 }
293
294
295
296
297 public WebAppContext(SecurityHandler securityHandler,SessionHandler sessionHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
298 {
299 super(null,
300 sessionHandler!=null?sessionHandler:new SessionHandler(),
301 securityHandler!=null?securityHandler:new SecurityHandler(),
302 servletHandler!=null?servletHandler:new ServletHandler(),
303 null);
304
305 setErrorHandler(errorHandler!=null?errorHandler:new ErrorPageErrorHandler());
306 }
307
308
309
310
311
312 public Throwable getUnavailableException()
313 {
314 return _unavailableException;
315 }
316
317
318
319
320
321
322
323
324
325
326 public void setResourceAlias(String alias, String uri)
327 {
328 if (_resourceAliases == null)
329 _resourceAliases= new HashMap(5);
330 _resourceAliases.put(alias, uri);
331 }
332
333
334 public Map getResourceAliases()
335 {
336 if (_resourceAliases == null)
337 return null;
338 return _resourceAliases;
339 }
340
341
342 public void setResourceAliases(Map map)
343 {
344 _resourceAliases = map;
345 }
346
347
348 public String getResourceAlias(String alias)
349 {
350 if (_resourceAliases == null)
351 return null;
352 return (String)_resourceAliases.get(alias);
353 }
354
355
356 public String removeResourceAlias(String alias)
357 {
358 if (_resourceAliases == null)
359 return null;
360 return (String)_resourceAliases.remove(alias);
361 }
362
363
364
365
366
367 public void setClassLoader(ClassLoader classLoader)
368 {
369 super.setClassLoader(classLoader);
370 if (classLoader!=null && classLoader instanceof WebAppClassLoader)
371 ((WebAppClassLoader)classLoader).setName(getDisplayName());
372 }
373
374
375 public Resource getResource(String uriInContext) throws MalformedURLException
376 {
377 IOException ioe= null;
378 Resource resource= null;
379 int loop=0;
380 while (uriInContext!=null && loop++<100)
381 {
382 try
383 {
384 resource= super.getResource(uriInContext);
385 if (resource != null && resource.exists())
386 return resource;
387
388 uriInContext = getResourceAlias(uriInContext);
389 }
390 catch (IOException e)
391 {
392 Log.ignore(e);
393 if (ioe==null)
394 ioe= e;
395 }
396 }
397
398 if (ioe != null && ioe instanceof MalformedURLException)
399 throw (MalformedURLException)ioe;
400
401 return resource;
402 }
403
404
405
406
407
408
409 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
410 throws IOException, ServletException
411 {
412 if (_unavailable)
413 {
414 response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
415 }
416 else
417 super.handle(target, request, response, dispatch);
418 }
419
420
421
422
423
424 protected void doStart() throws Exception
425 {
426 try
427 {
428
429 loadConfigurations();
430
431 for (int i=0;i<_configurations.length;i++)
432 _configurations[i].setWebAppContext(this);
433
434
435 _ownClassLoader=false;
436 if (getClassLoader()==null)
437 {
438 WebAppClassLoader classLoader = new WebAppClassLoader(this);
439 setClassLoader(classLoader);
440 _ownClassLoader=true;
441 }
442
443 if (Log.isDebugEnabled())
444 {
445 ClassLoader loader = getClassLoader();
446 Log.debug("Thread Context class loader is: " + loader);
447 loader=loader.getParent();
448 while(loader!=null)
449 {
450 Log.debug("Parent class loader is: " + loader);
451 loader=loader.getParent();
452 }
453 }
454
455 for (int i=0;i<_configurations.length;i++)
456 _configurations[i].configureClassLoader();
457
458 getTempDirectory();
459
460 super.doStart();
461
462 if (isLogUrlOnStart())
463 dumpUrl();
464 }
465 catch (Exception e)
466 {
467
468 Log.warn("Failed startup of context "+this, e);
469 _unavailableException=e;
470 _unavailable = true;
471 }
472 }
473
474
475
476
477
478 public void dumpUrl()
479 {
480 Connector[] connectors = getServer().getConnectors();
481 for (int i=0;i<connectors.length;i++)
482 {
483 String connectorName = connectors[i].getName();
484 String displayName = getDisplayName();
485 if (displayName == null)
486 displayName = "WebApp@"+connectors.hashCode();
487
488 Log.info(displayName + " at http://" + connectorName + getContextPath());
489 }
490 }
491
492
493
494
495
496 protected void doStop() throws Exception
497 {
498 super.doStop();
499
500 try
501 {
502
503 for (int i=_configurations.length;i-->0;)
504 _configurations[i].deconfigureWebApp();
505 _configurations=null;
506
507
508 if (_securityHandler.getHandler()==null)
509 {
510 _sessionHandler.setHandler(_securityHandler);
511 _securityHandler.setHandler(_servletHandler);
512 }
513
514
515 if (_tmpDir!=null && !_isExistingTmpDir && !isTempWorkDirectory())
516 {
517 IO.delete(_tmpDir);
518 _tmpDir=null;
519 }
520 }
521 finally
522 {
523 if (_ownClassLoader)
524 setClassLoader(null);
525
526 _unavailable = false;
527 _unavailableException=null;
528 }
529 }
530
531
532
533
534
535 public String[] getConfigurationClasses()
536 {
537 return _configurationClasses;
538 }
539
540
541
542
543
544 public Configuration[] getConfigurations()
545 {
546 return _configurations;
547 }
548
549
550
551
552
553
554 public String getDefaultsDescriptor()
555 {
556 return _defaultsDescriptor;
557 }
558
559
560
561
562
563
564 public String getOverrideDescriptor()
565 {
566 return _overrideDescriptor;
567 }
568
569
570
571
572
573 public PermissionCollection getPermissions()
574 {
575 return _permissions;
576 }
577
578
579
580
581
582
583 public String[] getServerClasses()
584 {
585 return _serverClasses;
586 }
587
588
589
590
591
592
593 public String[] getSystemClasses()
594 {
595 return _systemClasses;
596 }
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636 public File getTempDirectory()
637 {
638 if (_tmpDir!=null && _tmpDir.isDirectory() && _tmpDir.canWrite())
639 return _tmpDir;
640
641
642
643
644
645 Object t = getAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR);
646
647 if (t!=null && (t instanceof File))
648 {
649 _tmpDir=(File)t;
650 if (_tmpDir.isDirectory() && _tmpDir.canWrite())
651 return _tmpDir;
652 }
653
654 if (t!=null && (t instanceof String))
655 {
656 try
657 {
658 _tmpDir=new File((String)t);
659
660 if (_tmpDir.isDirectory() && _tmpDir.canWrite())
661 {
662 if(Log.isDebugEnabled())Log.debug("Converted to File "+_tmpDir+" for "+this);
663 setAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR,_tmpDir);
664 return _tmpDir;
665 }
666 }
667 catch(Exception e)
668 {
669 Log.warn(Log.EXCEPTION,e);
670 }
671 }
672
673
674 File work=null;
675 try
676 {
677 File w=new File(System.getProperty("jetty.home"),"work");
678 if (w.exists() && w.canWrite() && w.isDirectory())
679 work=w;
680 else if (getBaseResource()!=null)
681 {
682 Resource web_inf = getWebInf();
683 if (web_inf !=null && web_inf.exists())
684 {
685 w=new File(web_inf.getFile(),"work");
686 if (w.exists() && w.canWrite() && w.isDirectory())
687 work=w;
688 }
689 }
690 }
691 catch(Exception e)
692 {
693 Log.ignore(e);
694 }
695
696
697 try
698 {
699
700 String temp = getCanonicalNameForWebAppTmpDir();
701
702 if (work!=null)
703 _tmpDir=new File(work,temp);
704 else
705 {
706 _tmpDir=new File(System.getProperty("java.io.tmpdir"),temp);
707
708 if (_tmpDir.exists())
709 {
710 if(Log.isDebugEnabled())Log.debug("Delete existing temp dir "+_tmpDir+" for "+this);
711 if (!IO.delete(_tmpDir))
712 {
713 if(Log.isDebugEnabled())Log.debug("Failed to delete temp dir "+_tmpDir);
714 }
715
716 if (_tmpDir.exists())
717 {
718 String old=_tmpDir.toString();
719 _tmpDir=File.createTempFile(temp+"_","");
720 if (_tmpDir.exists())
721 _tmpDir.delete();
722 Log.warn("Can't reuse "+old+", using "+_tmpDir);
723 }
724 }
725 }
726
727 if (!_tmpDir.exists())
728 _tmpDir.mkdir();
729
730
731 if (!isTempWorkDirectory())
732 _tmpDir.deleteOnExit();
733 if(Log.isDebugEnabled())Log.debug("Created temp dir "+_tmpDir+" for "+this);
734 }
735 catch(Exception e)
736 {
737 _tmpDir=null;
738 Log.ignore(e);
739 }
740
741 if (_tmpDir==null)
742 {
743 try{
744
745 _tmpDir=File.createTempFile("JettyContext","");
746 if (_tmpDir.exists())
747 _tmpDir.delete();
748 _tmpDir.mkdir();
749 _tmpDir.deleteOnExit();
750 if(Log.isDebugEnabled())Log.debug("Created temp dir "+_tmpDir+" for "+this);
751 }
752 catch(IOException e)
753 {
754 Log.warn("tmpdir",e); System.exit(1);
755 }
756 }
757
758 setAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR,_tmpDir);
759 return _tmpDir;
760 }
761
762
763
764
765
766
767 public boolean isTempWorkDirectory ()
768 {
769 if (_tmpDir == null)
770 return false;
771 if (_tmpDir.getName().equalsIgnoreCase("work"))
772 return true;
773 File t = _tmpDir.getParentFile();
774 if (t == null)
775 return false;
776 return (t.getName().equalsIgnoreCase("work"));
777 }
778
779
780
781
782
783 public String getWar()
784 {
785 if (_war==null)
786 _war=getResourceBase();
787 return _war;
788 }
789
790
791 public Resource getWebInf() throws IOException
792 {
793 resolveWebApp();
794
795
796 Resource web_inf= super.getBaseResource().addPath("WEB-INF/");
797 if (!web_inf.exists() || !web_inf.isDirectory())
798 return null;
799
800 return web_inf;
801 }
802
803
804
805
806
807 public boolean isDistributable()
808 {
809 return _distributable;
810 }
811
812
813
814
815
816 public boolean isExtractWAR()
817 {
818 return _extractWAR;
819 }
820
821
822
823
824
825 public boolean isCopyWebDir()
826 {
827 return _copyDir;
828 }
829
830
831
832
833
834 public boolean isParentLoaderPriority()
835 {
836 return _parentLoaderPriority;
837 }
838
839
840 protected void loadConfigurations()
841 throws Exception
842 {
843 if (_configurations!=null)
844 return;
845 if (_configurationClasses==null)
846 _configurationClasses=__dftConfigurationClasses;
847
848 _configurations = new Configuration[_configurationClasses.length];
849 for (int i=0;i<_configurations.length;i++)
850 {
851 _configurations[i]=(Configuration)Loader.loadClass(this.getClass(), _configurationClasses[i]).newInstance();
852 }
853 }
854
855
856 protected boolean isProtectedTarget(String target)
857 {
858 while (target.startsWith("//"))
859 target=URIUtil.compactPath(target);
860
861 return StringUtil.startsWithIgnoreCase(target, "/web-inf") || StringUtil.startsWithIgnoreCase(target, "/meta-inf");
862 }
863
864
865
866 public String toString()
867 {
868 return this.getClass().getName()+"@"+Integer.toHexString(hashCode())+"{"+getContextPath()+","+(_war==null?getResourceBase():_war)+"}";
869 }
870
871
872
873
874
875
876 protected void resolveWebApp() throws IOException
877 {
878 Resource web_app = super.getBaseResource();
879 if (web_app == null)
880 {
881 if (_war==null || _war.length()==0)
882 _war=getResourceBase();
883
884
885 web_app= Resource.newResource(_war);
886
887
888 if (web_app.getAlias() != null)
889 {
890 Log.debug(web_app + " anti-aliased to " + web_app.getAlias());
891 web_app= Resource.newResource(web_app.getAlias());
892 }
893
894 if (Log.isDebugEnabled())
895 Log.debug("Try webapp=" + web_app + ", exists=" + web_app.exists() + ", directory=" + web_app.isDirectory());
896
897
898 if (web_app.exists() && !web_app.isDirectory() && !web_app.toString().startsWith("jar:"))
899 {
900
901 Resource jarWebApp= Resource.newResource("jar:" + web_app + "!/");
902 if (jarWebApp.exists() && jarWebApp.isDirectory())
903 {
904 web_app= jarWebApp;
905 }
906 }
907
908
909 if (web_app.exists() && (
910 (_copyDir && web_app.getFile()!= null && web_app.getFile().isDirectory())
911 ||
912 (_extractWAR && web_app.getFile()!= null && !web_app.getFile().isDirectory())
913 ||
914 (_extractWAR && web_app.getFile() == null)
915 ||
916 !web_app.isDirectory()
917 ))
918 {
919
920 File extractedWebAppDir= new File(getTempDirectory(), "webapp");
921
922 if (web_app.getFile()!=null && web_app.getFile().isDirectory())
923 {
924
925 Log.info("Copy " + web_app.getFile() + " to " + extractedWebAppDir);
926 IO.copyDir(web_app.getFile(),extractedWebAppDir);
927 }
928 else
929 {
930 if (!extractedWebAppDir.exists())
931 {
932
933 extractedWebAppDir.mkdir();
934 Log.info("Extract " + _war + " to " + extractedWebAppDir);
935 JarResource.extract(web_app, extractedWebAppDir, false);
936 }
937 else
938 {
939
940 if (web_app.lastModified() > extractedWebAppDir.lastModified())
941 {
942 extractedWebAppDir.delete();
943 extractedWebAppDir.mkdir();
944 Log.info("Extract " + _war + " to " + extractedWebAppDir);
945 JarResource.extract(web_app, extractedWebAppDir, false);
946 }
947 }
948 }
949
950 web_app= Resource.newResource(extractedWebAppDir.getCanonicalPath());
951
952 }
953
954
955 if (!web_app.exists() || !web_app.isDirectory())
956 {
957 Log.warn("Web application not found " + _war);
958 throw new java.io.FileNotFoundException(_war);
959 }
960
961 if (Log.isDebugEnabled())
962 Log.debug("webapp=" + web_app);
963
964
965 super.setBaseResource(web_app);
966 }
967 }
968
969
970
971
972
973
974
975 public void setConfigurationClasses(String[] configurations)
976 {
977 _configurationClasses = configurations==null?null:(String[])configurations.clone();
978 }
979
980
981
982
983
984 public void setConfigurations(Configuration[] configurations)
985 {
986 _configurations = configurations==null?null:(Configuration[])configurations.clone();
987 }
988
989
990
991
992
993
994 public void setDefaultsDescriptor(String defaultsDescriptor)
995 {
996 _defaultsDescriptor = defaultsDescriptor;
997 }
998
999
1000
1001
1002
1003
1004 public void setOverrideDescriptor(String overrideDescriptor)
1005 {
1006 _overrideDescriptor = overrideDescriptor;
1007 }
1008
1009
1010
1011
1012
1013 public String getDescriptor()
1014 {
1015 return _descriptor;
1016 }
1017
1018
1019
1020
1021
1022 public void setDescriptor(String descriptor)
1023 {
1024 _descriptor=descriptor;
1025 }
1026
1027
1028
1029
1030
1031 public void setDistributable(boolean distributable)
1032 {
1033 this._distributable = distributable;
1034 }
1035
1036
1037 public void setEventListeners(EventListener[] eventListeners)
1038 {
1039 if (_sessionHandler!=null)
1040 _sessionHandler.clearEventListeners();
1041
1042 super.setEventListeners(eventListeners);
1043
1044 for (int i=0; eventListeners!=null && i<eventListeners.length;i ++)
1045 {
1046 EventListener listener = eventListeners[i];
1047
1048 if ((listener instanceof HttpSessionActivationListener)
1049 || (listener instanceof HttpSessionAttributeListener)
1050 || (listener instanceof HttpSessionBindingListener)
1051 || (listener instanceof HttpSessionListener))
1052 {
1053 if (_sessionHandler!=null)
1054 _sessionHandler.addEventListener(listener);
1055 }
1056
1057 }
1058 }
1059
1060
1061
1062
1063
1064
1065 public void addEventListener(EventListener listener)
1066 {
1067 setEventListeners((EventListener[])LazyList.addToArray(getEventListeners(), listener, EventListener.class));
1068 }
1069
1070
1071
1072
1073
1074
1075 public void setExtractWAR(boolean extractWAR)
1076 {
1077 _extractWAR = extractWAR;
1078 }
1079
1080
1081
1082
1083
1084
1085 public void setCopyWebDir(boolean copy)
1086 {
1087 _copyDir = copy;
1088 }
1089
1090
1091
1092
1093
1094 public void setParentLoaderPriority(boolean java2compliant)
1095 {
1096 _parentLoaderPriority = java2compliant;
1097 }
1098
1099
1100
1101
1102
1103 public void setPermissions(PermissionCollection permissions)
1104 {
1105 _permissions = permissions;
1106 }
1107
1108
1109
1110
1111
1112 public void setServerClasses(String[] serverClasses)
1113 {
1114 _serverClasses = serverClasses==null?null:(String[])serverClasses.clone();
1115 }
1116
1117
1118
1119
1120
1121 public void setSystemClasses(String[] systemClasses)
1122 {
1123 _systemClasses = systemClasses==null?null:(String[])systemClasses.clone();
1124 }
1125
1126
1127
1128
1129
1130
1131
1132 public void setTempDirectory(File dir)
1133 {
1134 if (isStarted())
1135 throw new IllegalStateException("Started");
1136
1137 if (dir!=null)
1138 {
1139 try{dir=new File(dir.getCanonicalPath());}
1140 catch (IOException e){Log.warn(Log.EXCEPTION,e);}
1141 }
1142
1143 if (dir!=null && !dir.exists())
1144 {
1145 dir.mkdir();
1146 dir.deleteOnExit();
1147 }
1148 else if (dir != null)
1149 _isExistingTmpDir = true;
1150
1151 if (dir!=null && ( !dir.exists() || !dir.isDirectory() || !dir.canWrite()))
1152 throw new IllegalArgumentException("Bad temp directory: "+dir);
1153
1154 _tmpDir=dir;
1155 setAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR,_tmpDir);
1156 }
1157
1158
1159
1160
1161
1162 public void setWar(String war)
1163 {
1164 _war = war;
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174 public String getExtraClasspath()
1175 {
1176 return _extraClasspath;
1177 }
1178
1179
1180
1181
1182
1183
1184
1185 public void setExtraClasspath(String extraClasspath)
1186 {
1187 _extraClasspath=extraClasspath;
1188 }
1189
1190
1191 public boolean isLogUrlOnStart()
1192 {
1193 return _logUrlOnStart;
1194 }
1195
1196
1197
1198
1199
1200
1201
1202 public void setLogUrlOnStart(boolean logOnStart)
1203 {
1204 this._logUrlOnStart = logOnStart;
1205 }
1206
1207
1208 protected void startContext()
1209 throws Exception
1210 {
1211
1212 for (int i=0;i<_configurations.length;i++)
1213 _configurations[i].configureDefaults();
1214
1215
1216 Resource web_inf=getWebInf();
1217 if (web_inf!=null)
1218 {
1219 Resource work= web_inf.addPath("work");
1220 if (work.exists()
1221 && work.isDirectory()
1222 && work.getFile() != null
1223 && work.getFile().canWrite()
1224 && getAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR) == null)
1225 setAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR, work.getFile());
1226 }
1227
1228
1229 for (int i=0;i<_configurations.length;i++)
1230 _configurations[i].configureWebApp();
1231
1232
1233 super.startContext();
1234 }
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245 private String getCanonicalNameForWebAppTmpDir ()
1246 {
1247 StringBuffer canonicalName = new StringBuffer();
1248 canonicalName.append("Jetty");
1249
1250
1251 Connector[] connectors = getServer().getConnectors();
1252
1253
1254
1255 canonicalName.append("_");
1256 String host = (connectors==null||connectors[0]==null?"":connectors[0].getHost());
1257 if (host == null)
1258 host = "0.0.0.0";
1259 canonicalName.append(host.replace('.', '_'));
1260
1261
1262 canonicalName.append("_");
1263
1264 int port = (connectors==null||connectors[0]==null?0:connectors[0].getLocalPort());
1265
1266
1267 if (port < 0)
1268 port = connectors[0].getPort();
1269 canonicalName.append(port);
1270
1271
1272
1273 canonicalName.append("_");
1274 try
1275 {
1276 Resource resource = super.getBaseResource();
1277 if (resource == null)
1278 {
1279 if (_war==null || _war.length()==0)
1280 resource=Resource.newResource(getResourceBase());
1281
1282
1283 resource= Resource.newResource(_war);
1284 }
1285
1286 String tmp = URIUtil.decodePath(resource.getURL().getPath());
1287 if (tmp.endsWith("/"))
1288 tmp = tmp.substring(0, tmp.length()-1);
1289 if (tmp.endsWith("!"))
1290 tmp = tmp.substring(0, tmp.length() -1);
1291
1292 int i = tmp.lastIndexOf("/");
1293
1294 canonicalName.append(tmp.substring(i+1, tmp.length()));
1295 }
1296 catch (Exception e)
1297 {
1298 Log.warn("Can't generate resourceBase as part of webapp tmp dir name", e);
1299 }
1300
1301
1302 canonicalName.append("_");
1303 String contextPath = getContextPath();
1304 contextPath=contextPath.replace('/','_');
1305 contextPath=contextPath.replace('\\','_');
1306 canonicalName.append(contextPath);
1307
1308
1309 canonicalName.append("_");
1310 String[] vhosts = getVirtualHosts();
1311 canonicalName.append((vhosts==null||vhosts[0]==null?"":vhosts[0]));
1312
1313
1314 String hash = Integer.toString(canonicalName.toString().hashCode(),36);
1315 canonicalName.append("_");
1316 canonicalName.append(hash);
1317
1318
1319 for (int i=0;i<canonicalName.length();i++)
1320 {
1321 char c=canonicalName.charAt(i);
1322 if (!Character.isJavaIdentifierPart(c))
1323 canonicalName.setCharAt(i,'.');
1324 }
1325
1326 return canonicalName.toString();
1327 }
1328 }