1 19 20 package org.netbeans.modules.java.j2seproject.applet; 21 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.Tree; 24 import com.sun.source.util.TreePath; 25 import com.sun.source.util.Trees; 26 import java.io.*; 27 import java.net.*; 28 import java.util.*; 29 import javax.lang.model.element.Modifier; 30 import javax.lang.model.element.TypeElement; 31 import javax.lang.model.element.TypeElement; 32 import javax.lang.model.util.Elements; 33 import javax.lang.model.util.Types; 34 import org.netbeans.api.java.source.CancellableTask; 35 import org.netbeans.api.java.source.CompilationController; 36 import org.netbeans.api.java.source.JavaSource; 37 import org.netbeans.modules.java.j2seproject.J2SEProjectUtil; 38 39 import org.openide.*; 40 import org.openide.modules.SpecificationVersion; 41 import org.openide.filesystems.*; 42 import org.openide.util.*; 43 44 import org.netbeans.api.java.classpath.*; 45 import org.netbeans.spi.java.classpath.support.*; 46 import org.netbeans.api.java.queries.*; 47 import org.netbeans.api.project.*; 48 49 import org.netbeans.api.java.platform.*; 50 51 import org.openide.loaders.*; 52 import org.openide.cookies.*; 53 54 58 public class AppletSupport { 59 60 private static final SpecificationVersion JDK_15 = new SpecificationVersion("1.5"); 63 64 private static final String HTML_EXT = "html"; 66 private static final String CLASS_EXT = "class"; 68 private final static String POLICY_FILE_NAME = "applet"; 69 private final static String POLICY_FILE_EXT = "policy"; 70 71 private AppletSupport() {} 72 73 public static Boolean unitTestingSupport_isApplet = null; 76 77 public static boolean isApplet(final FileObject file) { 78 if (file == null) { 79 return false; 80 } 81 if (unitTestingSupport_isApplet != null) { 83 return unitTestingSupport_isApplet.booleanValue(); 84 } 85 JavaSource js = JavaSource.forFileObject(file); 86 if (js == null) { 87 return false; 88 } 89 final boolean[] result = new boolean[] {false}; 90 try { 91 js.runUserActionTask(new CancellableTask<CompilationController>() { 92 93 public void run(CompilationController control) throws Exception { 94 if (JavaSource.Phase.ELEMENTS_RESOLVED.compareTo(control.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED))<=0) { 95 Elements elements = control.getElements(); 96 Trees trees = control.getTrees(); 97 Types types = control.getTypes(); 98 TypeElement applet = elements.getTypeElement("java.applet.Applet"); TypeElement japplet = elements.getTypeElement("javax.swing.JApplet"); CompilationUnitTree cu = control.getCompilationUnit(); 101 List<? extends Tree> topLevels = cu.getTypeDecls(); 102 for (Tree topLevel : topLevels) { 103 if (topLevel.getKind() == Tree.Kind.CLASS) { 104 TypeElement type = (TypeElement) trees.getElement(TreePath.getPath(cu, topLevel)); 105 if (type != null) { 106 Set<Modifier> modifiers = type.getModifiers(); 107 if (modifiers.contains(Modifier.PUBLIC) && 108 ((applet != null && types.isSubtype(type.asType(), applet.asType())) 109 || (japplet != null && types.isSubtype(type.asType(), japplet.asType())))) { 110 result[0] = true; 111 break; 112 } 113 } 114 } 115 } 116 } 117 } 118 119 public void cancel() {} 120 }, true); 121 } catch (IOException ioe) { 122 Exceptions.printStackTrace(ioe); 123 } 124 return result[0]; 125 } 126 127 130 private static FileObject generateHtml(FileObject appletFile, FileObject buildDir, FileObject classesDir) throws IOException { 131 FileObject htmlFile = buildDir.getFileObject(appletFile.getName(), HTML_EXT); 132 133 if (htmlFile == null) { 134 htmlFile = buildDir.createData(appletFile.getName(), HTML_EXT); 135 } 136 137 FileLock lock = htmlFile.lock(); 138 PrintWriter writer = null; 139 try { 140 writer = new PrintWriter(htmlFile.getOutputStream(lock)); 141 ClassPath cp = ClassPath.getClassPath(appletFile, ClassPath.EXECUTE); 142 ClassPath sp = ClassPath.getClassPath(appletFile, ClassPath.SOURCE); 143 String path = FileUtil.getRelativePath(sp.findOwnerRoot(appletFile), appletFile); 144 path = path.substring(0, path.length()-5); 145 String codebase = FileUtil.getRelativePath(buildDir, classesDir); 146 if (codebase == null) { 147 codebase = classesDir.getURL().toString(); 148 } 149 fillInFile(writer, path + "." + CLASS_EXT, "codebase=\"" + codebase + "\""); } finally { 151 lock.releaseLock(); 152 if (writer != null) 153 writer.close(); 154 } 155 return htmlFile; 156 } 157 158 161 public static FileObject generateSecurityPolicy(FileObject projectDir) { 162 163 FileObject policyFile = projectDir.getFileObject(POLICY_FILE_NAME, POLICY_FILE_EXT); 164 165 try { 166 if (policyFile == null) { 167 policyFile = projectDir.createData(POLICY_FILE_NAME, POLICY_FILE_EXT); 168 } 169 FileLock lock = policyFile.lock(); 170 PrintWriter writer = null; 171 try { 172 writer = new PrintWriter(policyFile.getOutputStream(lock)); 173 fillInPolicyFile(writer); 174 } finally { 175 lock.releaseLock(); 176 if (writer != null) 177 writer.close(); 178 } 179 } catch (IOException ioe) { 180 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Problem when generating applet policy file: " + ioe); } 182 return policyFile; 183 } 184 185 188 public static URL generateHtmlFileURL(FileObject appletFile, FileObject buildDir, FileObject classesDir, String activePlatform) throws FileStateInvalidException { 189 FileObject html = null; 190 IOException ex = null; 191 if ((appletFile == null) || (buildDir == null) || (classesDir == null)) { 192 return null; 193 } 194 try { 195 html = generateHtml(appletFile, buildDir, classesDir); 196 if (html!=null) { 197 return getHTMLPageURL(html, activePlatform); 198 } 199 else { 200 return null; 201 } 202 } catch (IOException iex) { 203 return null; 204 } 205 } 206 207 208 215 public static URL getHTMLPageURL (FileObject htmlFile, String activePlatform) { 216 assert htmlFile != null : "htmlFile cannot be null"; JavaPlatform platform = J2SEProjectUtil.getActivePlatform(activePlatform); 219 boolean workAround6193279 = platform != null && platform.getSpecification().getVersion().compareTo(JDK_15)>=0; URL url = null; 222 if (workAround6193279) { 223 File f = FileUtil.toFile(htmlFile); 224 try { 225 String path = f.getAbsolutePath(); 226 if (File.separatorChar != '/') { path = path.replace(File.separatorChar,'/'); } 229 url = new URL ("file",null,path); 230 } catch (MalformedURLException e) { 231 ErrorManager.getDefault().notify(e); 232 } 233 } 234 else { 235 try { 236 url = htmlFile.getURL(); 237 } catch (FileStateInvalidException f) { 238 ErrorManager.getDefault().notify(f); 239 } 240 } 241 return url; 242 } 243 244 248 private static void fillInFile(PrintWriter writer, String name, String codebase) { 249 ResourceBundle bundle = NbBundle.getBundle(AppletSupport.class); 250 251 writer.println("<HTML>"); writer.println("<HEAD>"); 254 writer.print(" <TITLE>"); writer.print(bundle.getString("GEN_title")); 256 writer.println("</TITLE>"); 258 writer.println("</HEAD>"); writer.println("<BODY>\n"); 261 writer.print(bundle.getString("GEN_warning")); 262 263 writer.print("<H3><HR WIDTH=\"100%\">"); writer.print(bundle.getString("GEN_header")); 265 writer.println("<HR WIDTH=\"100%\"></H3>\n"); 267 writer.println("<P>"); if (codebase == null) 270 writer.print("<APPLET code="); else 272 writer.print("<APPLET " + codebase + " code="); writer.print ("\""); 275 writer.print(name); 276 writer.print ("\""); 278 writer.println(" width=350 height=200></APPLET>"); writer.println("</P>\n"); 281 writer.print("<HR WIDTH=\"100%\"><FONT SIZE=-1><I>"); writer.print(bundle.getString("GEN_copy")); 283 writer.println("</I></FONT>"); 285 writer.println("</BODY>"); writer.println("</HTML>"); writer.flush(); 288 } 289 290 291 292 295 private static void fillInPolicyFile(PrintWriter writer) { 296 writer.println("grant {"); writer.println("permission java.security.AllPermission;"); writer.println("};"); writer.flush(); 300 } 301 } 302 | Popular Tags |