KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > tools > DeploymentConfigurator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.tools;
21
22 import java.io.File JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.cayenne.project.DataNodeConfigInfo;
26 import org.apache.cayenne.project.ProjectConfigInfo;
27 import org.apache.cayenne.project.ProjectConfigurator;
28 import org.apache.cayenne.project.ProjectException;
29 import org.apache.cayenne.util.Util;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Task;
32
33 /**
34  * A "cdeploy" Ant task providing an Ant frontend to
35  * org.apache.cayenne.project.ProjectConfigurator.
36  *
37  * @author Andrus Adamchik
38  */

39 public class DeploymentConfigurator extends Task {
40
41     protected ProjectConfigInfo info;
42
43     /**
44      * Constructor for DeploymentConfigurator.
45      */

46     public DeploymentConfigurator() {
47         super();
48         info = new ProjectConfigInfo();
49     }
50
51     public ProjectConfigInfo getInfo() {
52         return info;
53     }
54
55     /**
56      * Executes the task. It will be called by ant framework.
57      */

58     public void execute() throws BuildException {
59         
60         validateAttributes();
61
62         try {
63             processProject();
64         }
65         catch (Exception JavaDoc ex) {
66             Throwable JavaDoc th = Util.unwindException(ex);
67             String JavaDoc message = th.getMessage();
68             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
69
70             if (message != null && message.trim().length() > 0) {
71                 buf.append("Error: [").append(message).append("].");
72             }
73             else {
74                 buf.append("Error reconfiguring jar file.");
75             }
76
77             buf
78                     .append(" Source: ")
79                     .append(info.getSourceJar())
80                     .append("; target: ")
81                     .append(info.getDestJar());
82
83             String JavaDoc errorMessage = buf.toString();
84             super.log(errorMessage);
85             throw new BuildException(errorMessage, ex);
86         }
87     }
88
89     /**
90      * Performs validation of task attributes. Throws BuildException if validation fails.
91      */

92     protected void validateAttributes() throws BuildException {
93         if (info.getSourceJar() == null) {
94             throw new BuildException("'src' attribute is required.");
95         }
96
97         if (!info.getSourceJar().isFile()) {
98             throw new BuildException("'src' must be a valid file: " + info.getSourceJar());
99         }
100
101         if (info.getAltProjectFile() != null && !info.getAltProjectFile().isFile()) {
102             throw new BuildException("'altProjectFile' must be a valid file: "
103                     + info.getAltProjectFile());
104         }
105
106         Iterator JavaDoc nodes = info.getNodes().iterator();
107         while (nodes.hasNext()) {
108             DataNodeConfigInfo node = (DataNodeConfigInfo) nodes.next();
109             if (node.getName() == null) {
110                 throw new BuildException("'node.name' attribute is required.");
111             }
112
113             if (node.getDataSource() != null && node.getDriverFile() != null) {
114                 throw new BuildException(
115                         "'node.dataSource' and 'node.driverFile' are mutually exclusive.");
116             }
117
118             if (node.getDriverFile() != null && !node.getDriverFile().isFile()) {
119                 throw new BuildException("'node.driverFile' does not exist.");
120             }
121         }
122     }
123
124     /**
125      * Performs the actual work on the project.
126      */

127     protected void processProject() throws ProjectException {
128         ProjectConfigurator conf = new ProjectConfigurator(info);
129         conf.execute();
130     }
131
132     public void setSrc(File JavaDoc file) {
133         info.setSourceJar(file);
134     }
135
136     public void setDest(File JavaDoc file) {
137         info.setDestJar(file);
138     }
139
140     public void setAltProjectFile(File JavaDoc file) {
141         info.setAltProjectFile(file);
142     }
143
144     public void addNode(DataNodeConfigInfo node) {
145         info.addToNodes(node);
146     }
147 }
148
Popular Tags