KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > nature > HibernateNature


1 package org.hibernate.eclipse.nature;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.eclipse.core.resources.ICommand;
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.IProjectDescription;
12 import org.eclipse.core.resources.IProjectNature;
13 import org.eclipse.core.resources.ProjectScope;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.core.runtime.preferences.IScopeContext;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.hibernate.cfg.JDBCMetaDataConfiguration;
25 import org.hibernate.console.ConsoleConfiguration;
26 import org.hibernate.console.KnownConfigurations;
27 import org.hibernate.console.ConsoleConfiguration.Command;
28 import org.hibernate.eclipse.builder.HibernateBuilder;
29 import org.hibernate.eclipse.console.HibernateConsolePlugin;
30 import org.hibernate.mapping.Table;
31 import org.osgi.service.prefs.Preferences;
32
33 public class HibernateNature implements IProjectNature {
34
35     final public static String JavaDoc ID = "org.hibernate.eclipse.console.hibernateNature";
36     
37     private IProject project;
38
39     public void configure() throws CoreException {
40         HibernateConsolePlugin.getDefault().log("Configuring " + project + " as a Hibernate project");
41         
42         IProjectDescription desc = project.getDescription();
43            ICommand[] commands = desc.getBuildSpec();
44            boolean found = false;
45
46            for (int i = 0; i < commands.length; ++i) {
47               if (commands[i].getBuilderName().equals(HibernateBuilder.BUILDER_ID)) {
48                  found = true;
49                  break;
50               }
51            }
52            if (!found) {
53               //add builder to project
54
ICommand command = desc.newCommand();
55               command.setBuilderName(HibernateBuilder.BUILDER_ID);
56               ArrayList JavaDoc list = new ArrayList JavaDoc();
57               list.addAll(Arrays.asList(commands));
58               list.add(command);
59               desc.setBuildSpec((ICommand[])list.toArray(new ICommand[]{}));
60               project.setDescription(desc, new NullProgressMonitor());
61            }
62     }
63
64     public void deconfigure() throws CoreException {
65         HibernateConsolePlugin.getDefault().log("Deconfiguring " + project + " as a Hibernate project");
66     }
67
68     public IProject getProject() {
69         return project;
70     }
71
72     public void setProject(IProject project) {
73         this.project = project;
74     }
75
76     public ConsoleConfiguration getDefaultConsoleConfiguration() {
77             String JavaDoc cfg = getDefaultConsoleConfigurationName();
78             ConsoleConfiguration configuration = KnownConfigurations.getInstance().find(cfg);
79             return configuration;
80     }
81     
82     public String JavaDoc getDefaultConsoleConfigurationName() {
83         IJavaProject prj = JavaCore.create(project);
84         IScopeContext scope = new ProjectScope(prj.getProject());
85         
86         Preferences node = scope.getNode("org.hibernate.eclipse.console");
87         
88         if(node!=null) {
89             String JavaDoc cfg = node.get("default.configuration", prj.getProject().getName());
90             return cfg;
91         } else {
92             return null;
93         }
94     }
95     
96     List JavaDoc tables = null;
97
98     private ReadDatabaseMetaData job;
99     
100     public List JavaDoc getTables() {
101         ConsoleConfiguration ccfg = getDefaultConsoleConfiguration();
102         if(ccfg==null) return Collections.EMPTY_LIST;
103         
104         if(tables!=null) {
105             return tables;
106         } else {
107             if(job==null) {
108                 job = new ReadDatabaseMetaData(ccfg);
109                 job.setPriority(Job.DECORATE);
110                 job.schedule();
111             }
112             return Collections.EMPTY_LIST;
113         }
114     }
115     
116     public class ReadDatabaseMetaData extends Job {
117         
118         private ConsoleConfiguration ccfg;
119
120         public ReadDatabaseMetaData(ConsoleConfiguration ccfg) {
121             super("Reading database metadata for " + getProject().getName());
122             this.ccfg = ccfg;
123         }
124         
125         protected IStatus run(IProgressMonitor monitor) {
126             final JDBCMetaDataConfiguration jcfg = (JDBCMetaDataConfiguration) ccfg.buildWith(new JDBCMetaDataConfiguration(), false);
127             monitor.beginTask("Reading database metadata", IProgressMonitor.UNKNOWN);
128             try {
129                 ccfg.execute(new Command() {
130                     public Object JavaDoc execute() {
131                         jcfg.readFromJDBC();
132                         return null;
133                     }
134                 });
135                 
136                 
137                 List JavaDoc result = new ArrayList JavaDoc();
138                 Iterator JavaDoc tabs = jcfg.getTableMappings();
139                 
140                 while (tabs.hasNext()) {
141                     Table table = (Table) tabs.next();
142                     monitor.subTask(table.getName());
143                     result.add(table);
144                 }
145                 
146                 tables = result;
147                 monitor.done();
148                 return Status.OK_STATUS;
149             } catch(Throwable JavaDoc t) {
150                 return new Status(Status.ERROR, HibernateConsolePlugin.ID, 1, "Error while performing background reading of database schema", t);
151             }
152         }
153
154     }
155 }
156
Popular Tags