001 /* 002 * Jpkg - Java library and tools for operating system package creation. 003 * 004 * Copyright (c) 2007-2008 Three Rings Design, Inc. 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without 008 * modification, are permitted provided that the following conditions 009 * are met: 010 * 1. Redistributions of source code must retain the above copyright 011 * notice, this list of conditions and the following disclaimer. 012 * 2. Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in the 014 * documentation and/or other materials provided with the distribution. 015 * 3. Neither the name of the copyright owner nor the names of contributors 016 * may be used to endorse or promote products derived from this software 017 * without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 029 * POSSIBILITY OF SUCH DAMAGE. 030 */ 031 package com.threerings.jpkg.ant.dpkg.scripts.standard; 032 033 import java.io.FileInputStream; 034 import java.io.IOException; 035 import java.io.InputStream; 036 037 import com.threerings.antidote.property.BooleanProperty; 038 import com.threerings.antidote.property.FileProperty; 039 import com.threerings.antidote.property.StringProperty; 040 import com.threerings.jpkg.ant.dpkg.DpkgData; 041 import com.threerings.jpkg.ant.dpkg.scripts.TemplateScript; 042 import com.threerings.jpkg.debian.MaintainerScript; 043 044 import static com.threerings.antidote.MutabilityHelper.areMutablesSet; 045 046 047 /** 048 * An abstract {@link TemplateScript} for defining the script source from either a file or a 049 * single command listed as a string. 050 */ 051 public abstract class AbstractTypeScript extends TemplateScript 052 { 053 public AbstractTypeScript (MaintainerScript.Type type) 054 { 055 super(type); 056 } 057 058 // from VelocityTemplate 059 public String getTemplateName () 060 { 061 return "scripts/command_script.vm"; 062 } 063 064 @Override // from TemplateScript 065 public InputStream getSource (DpkgData data) 066 throws IOException 067 { 068 // if the command property was set, return the velocity template. 069 if (_command.isSet()) { 070 addSubstitution("command", _command.getValue()); 071 return super.getSource(data); 072 073 // otherwise return the file data. 074 } else { 075 return new FileInputStream(_source.getValue()); 076 } 077 } 078 079 // from PackageScript 080 public boolean failOnError () 081 { 082 return _failonerror.getValue(); 083 } 084 085 /** 086 * Ant setter field: the command line. 087 */ 088 public void setCommand (String value) 089 { 090 _command.setValue(value); 091 } 092 093 /** 094 * Ant setter field: the source file. 095 */ 096 public void setSource (String value) 097 { 098 _source.setValue(value); 099 } 100 101 /** 102 * Ant setter field: failonerror. 103 */ 104 public void setFailonerror (String value) 105 { 106 _failonerror.setValue(value); 107 } 108 109 @Override // from BaseComponent 110 protected void validateField () 111 { 112 switch (areMutablesSet(_command, _source)) { 113 case ALL_UNSET: 114 appendViolation(new UnsetScriptPropertiesViolation(this)); 115 return; 116 117 case ALL_SET: 118 reportConflictingProperties(_command, _source); 119 reportConflictingProperties(_source, _command); 120 return; 121 122 case SOME_UNSET: 123 // only one property is set, which is correct. 124 break; 125 } 126 127 // validate required properties. failonerror defaults to true if not set. 128 switch (validateProperties(_failonerror)) { 129 case ALL_INVALID: 130 case SOME_INVALID: 131 return; 132 133 case ALL_VALID: 134 break; 135 } 136 137 // validate optional properties. 138 switch (validateOptionalProperties(_command, _source)) { 139 case ALL_INVALID: 140 case SOME_INVALID: 141 return; 142 143 case ALL_VALID: 144 break; 145 } 146 } 147 148 /** Ant adder/setter fields. */ 149 private final StringProperty _command = new StringProperty("command", this); 150 private final FileProperty _source = new FileProperty("source", this); 151 private final BooleanProperty _failonerror = new BooleanProperty("failonerror", this, true); 152 }