001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.runtime.util; 018 019 import java.io.ByteArrayInputStream; 020 import java.io.File; 021 import java.io.InputStream; 022 import java.net.MalformedURLException; 023 import java.net.URL; 024 025 /** 026 * Container which provides defails about a Groovy scripts source. 027 * 028 * @version $Id: ClassSource.java 12 2009-07-16 09:27:15Z user57 $ 029 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 030 */ 031 public class ClassSource 032 { 033 public final URL url; 034 035 public final File file; 036 037 public final Body body; 038 039 private ClassSource(final URL url, final File file, final Body body) { 040 this.url = url; 041 this.file = file; 042 this.body = body; 043 } 044 045 public ClassSource(final URL url) { 046 this(url, null, null); 047 } 048 049 public ClassSource(final File file) { 050 this(null, file, null); 051 } 052 053 public ClassSource(final Body body) { 054 this(null, null, body); 055 } 056 057 public String toString() { 058 return "ClassSource" + 059 "[ url=" + url + 060 ", file=" + file + 061 ", body=" + body + 062 " ]"; 063 } 064 065 // 066 // Body 067 // 068 069 public static class Body 070 { 071 public final String name; 072 073 public final String codeBase; 074 075 public final InputStream input; 076 077 public Body(final String name, final String codeBase, final InputStream input) { 078 assert name != null; 079 assert codeBase != null; 080 assert input != null; 081 082 this.name = name; 083 this.codeBase = codeBase; 084 this.input = input; 085 } 086 087 public Body(final String source) { 088 this("script" + System.currentTimeMillis() + ".groovy", 089 "/groovy/script", 090 new ByteArrayInputStream(source.getBytes())); 091 } 092 093 public String toString() { 094 return "Body" + 095 "[ name=" + name + 096 ", codeBase=" + codeBase + 097 ", input=" + input + 098 " ]"; 099 } 100 } 101 102 // 103 // Helpers 104 // 105 106 public static ClassSource forValue(final String source) { 107 assert source != null; 108 109 // First try and parse the source as a URL 110 try { 111 return new ClassSource(new URL(source)); 112 } 113 catch (MalformedURLException ignore) {} 114 115 // Then as a File 116 File file = new File(source); 117 if (file.exists()) { 118 return new ClassSource(file); 119 } 120 121 // Else its a body 122 return new ClassSource(new ClassSource.Body(source)); 123 } 124 }