KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdrant > Instantiate


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdrant;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25
26 import javax.jmi.model.MofPackage;
27 import javax.jmi.model.ModelPackage;
28
29 /** Instantiates metamodel into given extent.
30  *
31  * @author Petr Hrebejk
32  */

33 public class Instantiate extends MdrTask.Sub {
34
35     private String JavaDoc name;
36     private String JavaDoc extent;
37     private String JavaDoc packageName;
38
39     /** Creates a new instance of PrintExtentsNames */
40     public Instantiate() {
41     }
42
43     public void execute() throws Exception JavaDoc {
44
45         if ( name == null ) {
46             throw new BuildException( "Name for newly instantiated package has to be sppecified. Use the \"name\" attribute" );
47         }
48         
49         if ( extent == null && packageName == null ) {
50             getRepository().createExtent( name );
51         }
52         else {
53             if ( extent == null ) {
54                 throw new BuildException( "Extent containing the package has to be specified. Use the \"extent\" attribute" );
55             }
56             if ( packageName == null ) {
57                 throw new BuildException( "The package has to be specified. Use the \"package\" attribute" );
58             }
59             
60             MofPackage mp = getModelPackage();
61             
62             if ( mp == null ) {
63                 throw new BuildException( packageName + " does not exist in extent " + extent );
64             }
65             
66             getRepository().createExtent( name, mp );
67         }
68         
69     }
70     
71     // ANT task attributes -----------------------------------------------------
72

73     public void setName( String JavaDoc name ) {
74         this.name = name;
75     }
76     
77     public void setExtent( String JavaDoc extent ) {
78         this.extent = extent;
79     }
80     
81     public void setPackage( String JavaDoc packageName ) {
82         this.packageName = packageName;
83     }
84     
85     // private methods ---------------------------------------------------------
86

87     
88     private MofPackage getModelPackage() {
89         
90         ModelPackage mofPackage = (ModelPackage)getRepository().getExtent( extent );
91         MofPackage result;
92         
93         for (Iterator JavaDoc it = mofPackage.getMofPackage().refAllOfClass().iterator(); it.hasNext();) {
94             result = (MofPackage) it.next();
95             if (result.getName().equals( packageName )) {
96                 return result;
97             }
98         }
99         
100         return null;
101     }
102     
103     
104 }
105
Popular Tags