[jboss-cvs] jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/module ...

Robert Stryker rawblem at gmail.com
Thu Nov 9 19:26:26 EST 2006


  User: rawb    
  Date: 06/11/09 19:26:26

  Added:       as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/module    
                        JBossModuleDelegate.java JBossModuleFactory.java
                        ArchiveModuleFactory.java
                        ArchiveModuleArtifactAdapter.java
  Log:
  Rewrote Core entirley. It's clean and spiffy now. 
  
  Revision  Changes    Path
  1.1      date: 2006/11/10 00:26:26;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/module/JBossModuleDelegate.java
  
  Index: JBossModuleDelegate.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.module;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.jar.JarEntry;
  import java.util.jar.JarFile;
  
  import org.dom4j.Document;
  import org.dom4j.io.SAXReader;
  import org.dom4j.tree.DefaultElement;
  import org.eclipse.core.runtime.IStatus;
  import org.eclipse.core.runtime.Path;
  import org.eclipse.wst.server.core.IModule;
  import org.eclipse.wst.server.core.model.ModuleDelegate;
  
  
  /**
   * This is the module delegate abstract superclass for any JBoss Module.
   * It is expected that the module-specific delegate class is defined
   * in the factory class itself.
   * 
   * @author rstryker
   *
   */
  public abstract class JBossModuleDelegate extends ModuleDelegate {
  
  	private static Integer DNE = new Integer(-1); 	
  
  	private JBossModuleFactory factory;
  	private int touched = 0;
  	
  	/**
  	 * References to the resource location, in path and URL format
  	 */
  	private String resourcePath;
  	private URL resourceURL;
  	
  	/**
  	 * Map to hold xml documents within our module
  	 */
  	private HashMap entryToDocument;
  	
  	public JBossModuleDelegate() {
  		entryToDocument = new HashMap();
  	}
  
  	
  	public abstract void initialize();	
  	public abstract IModule[] getChildModules();
  	public abstract IStatus validate();
  
  
  	/**
  	 * Some junk implementation to make it easy to "touch" a module
  	 * and mark it as changed.
  	 */
  //	public IModuleResource[] members() throws CoreException {
  //		ModuleFile f = new ModuleFile(TOUCHED_NAME, new Path(TOUCHED_PATH).append(""+touched), touched);
  //		return new IModuleResource[]{ f };
  //	}
  
  	public void touchModule() {
  		touched++;
  	}
  	
  	public void setFactory(JBossModuleFactory factory) {
  		this.factory = factory;
  	}
  	
  	public JBossModuleFactory getFactory() {
  		return this.factory;
  	}
  	
  	public String getResourcePath() {
  		return resourcePath;
  	}
  
  	public String getResourceName() {
  		Path p = new Path(getResourcePath());
  		String[] segments = p.segments();
  		return segments[segments.length - 1];
  	}
  	
  	public void setResourcePath(String path) {
  		try {
  			this.resourcePath = path;
  			this.resourceURL = new File(path).toURL();
  		} catch( Exception e ) {
  		}
  	}
  	
  	/**
  	 * Return a Document object for some xml descriptor
  	 * within this module. If the descriptor hasn't been parsed
  	 * yet, parse it now.
  	 * 
  	 * @param entryName
  	 * @return
  	 */
  	protected Document getDocument(String entryName) {
  		Object o = entryToDocument.get(entryName);
  		if( o == DNE ) return null;
  		if( o != null ) return (Document)o;
  		
  		// Does the entry exist
  		if( !entryExists(entryName)) {
  			entryToDocument.put(entryName, DNE);
  			return null;
  		}
  		
  		try {
  			// Load the entry
  
  			String url = "jar:" + this.resourceURL.toString() + "!/" + entryName;
  			URL url2 = new URL(url);
  			SAXReader reader = new SAXReader(false);
  			InputStream is = url2.openStream();
  			Document document = reader.read(is);
  			entryToDocument.put(entryName, document);
  			is.close();
  			return document;
  			
  		} catch( Exception e ) {
  			e.printStackTrace();
  		}
  		
  		
  		return null;
  	}
  	
  	
  	/**
  	 * Search through a descriptor file jarentry for the 
  	 * designated xpath. 
  	 * 
  	 * If the entry hasn't been parsed into a Document yet, 
  	 * parse it now.
  	 * 
  	 * @param entry
  	 * @param xpath
  	 * @return
  	 */
  	public String[] getXpathValues(String entry, String xpath) {
  		if( entryExists(entry)) {
  			ArrayList strings = new ArrayList();
  			try {
  				Document d = getDocument(entry);
  				List l = d.selectNodes(xpath);
  				Iterator i = l.iterator();
  				while(i.hasNext() ) {
  					DefaultElement el = (DefaultElement)i.next();
  					strings.add(el.getText());
  				}
  			} catch( Throwable e ) {
  				e.printStackTrace();
  			}
  			
  			String[] retval = new String[strings.size()];
  			strings.toArray(retval);
  			return retval;
  		} else {
  			return new String[] { };
  		}
  	}
  	
  	/**
  	 * Return whether the entry exists in our module
  	 * @param entryName
  	 * @return
  	 */
  	public boolean entryExists(String entryName) {
  		try {
  			File f = new File(resourcePath);
  			JarFile jf = new JarFile(f);
  			
  			JarEntry entry = jf.getJarEntry(entryName);
  			jf.close();
  			if( entry == null ) return false;
  			return true;
  		} catch( IOException ioe ) {
  		}
  		return false;
  	}
  
  	/**
  	 * Clear all document references
  	 */
  	public void clearDocuments() {
  		entryToDocument.clear();
  	}
  }
  
  
  
  1.1      date: 2006/11/10 00:26:26;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/module/JBossModuleFactory.java
  
  Index: JBossModuleFactory.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.module;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  
  import org.eclipse.core.resources.IResource;
  import org.eclipse.core.resources.IResourceChangeEvent;
  import org.eclipse.core.resources.IResourceChangeListener;
  import org.eclipse.core.resources.IResourceDelta;
  import org.eclipse.core.resources.IResourceDeltaVisitor;
  import org.eclipse.core.resources.ResourcesPlugin;
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.wst.server.core.IModule;
  import org.eclipse.wst.server.core.IServer;
  import org.eclipse.wst.server.core.ServerCore;
  import org.eclipse.wst.server.core.internal.Server;
  import org.eclipse.wst.server.core.model.ModuleDelegate;
  import org.eclipse.wst.server.core.model.ModuleFactoryDelegate;
  import org.jboss.ide.eclipse.as.core.JBossServerCore;
  import org.jboss.ide.eclipse.as.core.model.ModuleModel;
  import org.jboss.ide.eclipse.as.core.server.JBossServer;
  
  public abstract class JBossModuleFactory extends ModuleFactoryDelegate {
  	
  	public static final String NO_LOCATION = "__NO_LOCATION__";
  	
  	protected HashMap pathToModule = null;
  	protected HashMap moduleToDelegate = null;
  	
  	public JBossModuleFactory() {
  		pathToModule = new HashMap();
  		moduleToDelegate = new HashMap();
  		initialize();
  	}
  	
  	public abstract void initialize();
  	public abstract boolean supports(String path);
  	
  	public boolean supports( IResource resource ) {
  		return supports(getPath(resource));
  	}
  	
  	/**
  	 * Get a delegate for this module. 
  	 */
  	public ModuleDelegate getModuleDelegate(IModule module) {
  		return (ModuleDelegate)moduleToDelegate.get(module);
  	}
  
  	/**
  	 * Get a list of all modules this factory has references to.
  	 */
  	public IModule[] getModules() {		
  		if( pathToModule == null ) {
  			cacheModules();
  		}
  		Collection modules = pathToModule.values();
  
  		IModule[] modules2 = new IModule[modules.size()];
  		modules.toArray(modules2);
  		return modules2;
  	}
  	
  	/**
  	 * Get a list of minimal modules to have on hand, 
  	 * specifically those that are already deployed to a server
  	 * and are expected. 
  	 */
  	protected void cacheModules() {
  		this.pathToModule = new HashMap();
  		this.moduleToDelegate = new HashMap();
  		
  		String[] paths = getServerModulePaths();
  		for( int i = 0; i < paths.length; i++ ) {
  			acceptAddition(paths[i]);
  		}
  	}
  	
  	// lifted from other class
  	private String[] getServerModulePaths() {
  		// Stolen from Server.class, not public
  		final String MODULE_LIST = "modules";
  
  		ArrayList paths = new ArrayList();
  		IServer[] server = ServerCore.getServers();
  		for( int i = 0; i < server.length; i++ ) {
  			if( server[i].getClass().equals(Server.class)) {
  				// Get the module ID list:
  				List l = ((Server)server[i]).getAttribute(MODULE_LIST, (List) null);
  
  				if( l == null ) return new String[0];
  				
  				Object[] o = l.toArray();
  				// Get the module ID list:
  				for( int j = 0; j < o.length; j++ ) {
  					String moduleId = (String) o[j];
  					String name = "<unknown>";
  					int index = moduleId.indexOf("::");
  					if (index > 0) {
  						name = moduleId.substring(0, index);
  						moduleId = moduleId.substring(index+2);
  					}
  					
  					String moduleTypeId = null;
  					String moduleTypeVersion = null;
  					index = moduleId.indexOf("::");
  					if (index > 0) {
  						int index2 = moduleId.indexOf("::", index+1);
  						moduleTypeId = moduleId.substring(index+2, index2);
  						moduleTypeVersion = moduleId.substring(index2+2);
  						moduleId = moduleId.substring(0, index);
  					}
  					
  					if( moduleId.startsWith(getFactoryId() + ":")) {
  						String path = moduleId.substring((getFactoryId()+":").length());
  						paths.add(path);
  					}
  					
  				}
  			}
  		}
  		return (String[]) paths.toArray(new String[paths.size()]);
  	}
  	
  	/**
  	 * Return an associated module for this resource, 
  	 * or null of none is found in this factory.
  	 * @param resource
  	 * @return
  	 */
  	public IModule getModule(String path) {
  		//ASDebug.p("getModule: " + resource.getFullPath() + ", my ID is " + getId(), this);
  		if(pathToModule == null) {
  			cacheModules();
  		}
  		
  		// return the module if it already exists
  		//String path = getPath(resource);
  		if( pathToModule.get(path) != null ) {
  			return (IModule)pathToModule.get(path);
  		}
  		if( supports(path)) {
  			acceptAddition(path);
  			if( pathToModule.get(path) != null ) {
  				return (IModule)pathToModule.get(path);
  			}
  		}
  		return null;
  	}
  	
  	public IModule getModule(IResource resource) {
  		return getModule(getPath(resource));
  	}
  	
  	/**
  	 * I dont think any class uses this yet, but I thought it should be public.
  	 * @return
  	 */
  	public String getFactoryId() {
  		return getId();
  	}
  	
  	protected abstract IModule acceptAddition(String path);
  	
  	/**
  	 * Handle a deleted resource.
  	 * For now, we're removing the module from the factory entirely.
  	 * This will mean the server has a reference to a module it cannot find, but
  	 * the module will still be on the server. 
  	 * @param resource
  	 */
  	protected void acceptDeletion(String resourcePath) {
  		IModule module = getModule(resourcePath);
  		if( module == null ) return;
  		
  		Object delegate = moduleToDelegate.get(module);
  		moduleToDelegate.remove(module);
  		pathToModule.remove(resourcePath);
  		// TODO Mark removed in a model
  		ModuleModel.getDefault().markModuleChanged(module);
  	}
  
  	protected void acceptChange(String resourcePath) {
  		IModule module = getModule(resourcePath);
  		if( module == null ) return;
  		
  		Object delegate = moduleToDelegate.get(module);
  		// TODO Mark change in some model 
  		ModuleModel.getDefault().markModuleChanged(module);
  	}
  	
  	
  	public void resourceEvent(IResource resource, int kind) {
  		switch( kind ) {
  			case IResourceDelta.REMOVED:
  				if( contains(getPath(resource))) 
  					acceptDeletion(getPath(resource));
  				break;
  			case IResourceDelta.ADDED:
  				acceptAddition(getPath(resource));
  				break;
  			case IResourceDelta.CHANGED:
  				if( contains(getPath(resource))) 
  					acceptChange(getPath(resource));
  				break;
  			default:
  				break;
  		}
  		
  	}
  	
  	/**
  	 * Does this factory already have a reference to the resource?
  	 * @param resource
  	 * @return
  	 */
  	protected boolean contains(String resourcePath) {
  		if(pathToModule == null) {
  			cacheModules();
  		}
  
  		Object o = pathToModule.get(resourcePath);
  		return o == null ? false : true;
  	}
  	
  	
  	/**
  	 * Return the absolute location of the resource, 
  	 * or a constant indicating not found
  	 * @param resource
  	 * @return
  	 */
  	public String getPath(IResource resource) {
  		try {
  			return resource.getLocation().toOSString();
  		} catch( Exception e ) {
  			return NO_LOCATION;
  		}
  	}
  	
  	
  }
  
  
  
  1.1      date: 2006/11/10 00:26:26;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/module/ArchiveModuleFactory.java
  
  Index: ArchiveModuleFactory.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.module;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.jar.JarFile;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IStatus;
  import org.eclipse.core.runtime.Path;
  import org.eclipse.core.runtime.Status;
  import org.eclipse.wst.server.core.IModule;
  import org.eclipse.wst.server.core.internal.ServerPlugin;
  import org.eclipse.wst.server.core.model.IModuleResource;
  import org.jboss.ide.eclipse.as.core.JBossServerCorePlugin;
  
  public class ArchiveModuleFactory extends JBossModuleFactory {
  	
  	private static String GENERIC_JAR = "jboss.archive";
  	private static String VERSION = "1.0";
  	
  	public static final String FACTORY_ID = "org.jboss.ide.eclipse.as.core.ArchiveFactory";
  
  	private static ArchiveModuleFactory factory;
  	public static ArchiveModuleFactory getDefault() {
  		return factory;
  	}
  	
  	public ArchiveModuleFactory() {
  		factory = this;
  	}
  
  	public void initialize() {
  	}
  
  	protected IModule acceptAddition(String path) {
  		if( !supports(path)) 
  			return null;
  
  		// otherwise create the module
  		//String path = getPath(resource);
  		String name = new Path(path).lastSegment();
  		IModule module = createModule(path, name, 
  				GENERIC_JAR, VERSION, null);
  		
  		
  		ArchiveModuleDelegate delegate = new ArchiveModuleDelegate();
  		delegate.initialize(module);
  		delegate.setResourcePath(path);
  		delegate.setFactory(this);
  		
  		// and insert it
  		pathToModule.put(path, module);
  		moduleToDelegate.put(module, delegate);
  		
  		// ensure the factory clears its cache
  		clearModuleCache();
  		
  		return module;	
  		
  	}
  
  	public boolean supports(String path) {
  		try {
  			File f = new File(path);
  			JarFile jf = new JarFile(f);
  			return true;
  		} catch( IOException e ) {
  		}
  		return false;
  	}
  	
  	
  	public class ArchiveModuleDelegate extends JBossModuleDelegate {
  		public IModule[] getChildModules() {
  			return null;
  		}
  
  		public void initialize() {
  		}
  
  		public IStatus validate() {
  			return new Status(IStatus.OK, JBossServerCorePlugin.PLUGIN_ID, 
  					0, "Deployment is valid", null);
  		}
  
  		public IModuleResource[] members() throws CoreException {
  			return new IModuleResource[0];
  		}
  	}
  }
  
  
  
  1.1      date: 2006/11/10 00:26:26;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/module/ArchiveModuleArtifactAdapter.java
  
  Index: ArchiveModuleArtifactAdapter.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.module;
  
  import org.eclipse.core.resources.IResource;
  import org.eclipse.core.runtime.NullProgressMonitor;
  import org.eclipse.wst.server.core.IModule;
  import org.eclipse.wst.server.core.IModuleArtifact;
  import org.eclipse.wst.server.core.internal.ModuleFactory;
  import org.eclipse.wst.server.core.internal.ServerPlugin;
  import org.eclipse.wst.server.core.model.ModuleArtifactAdapterDelegate;
  
  public class ArchiveModuleArtifactAdapter extends ModuleArtifactAdapterDelegate {
  	public IModuleArtifact getModuleArtifact(Object obj) {
  		if( obj instanceof IResource ) {
  			try {
  				IResource res = (IResource)obj;
  				if( ArchiveModuleFactory.getDefault() == null ) {
  					ModuleFactory[] factories = ServerPlugin.getModuleFactories(); // just make sure they're loaded
  					for( int i = 0; i < factories.length; i++ ) {
  						if( factories[i].getId().equals(ArchiveModuleFactory.FACTORY_ID)) {
  							factories[i].getDelegate(new NullProgressMonitor());
  						}
  					}
  				}
  				if( !ArchiveModuleFactory.getDefault().supports(res)) return null;
  				
  				final IModule mod = ArchiveModuleFactory.getDefault().getModule(res);
  				return new IModuleArtifact() {
  					public IModule getModule() {
  						return mod;
  					} 
  				};
  			} catch( Throwable t ) {
  				t.printStackTrace();
  			}
  		}
  		return null;
  	}
  }
  
  



More information about the jboss-cvs-commits mailing list