alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package model;

import dk.jaoo.abwsclient.opc.purchaseorder.*;
import java.rmi.RemoteException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.xml.rpc.ServiceException;
import org.eclipse.core.runtime.*;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.ui.examples.rcp.adventure.*;
import org.eclipse.ui.examples.rcp.adventure.preferences.IPreferenceConstants;
import org.eclipse.ui.examples.rcp.adventure.util.PurchaseOrderUtil;
import org.osgi.framework.BundleContext;

/**
 * Central model class for the Go Wild Travel (Adventure Builder) application.
 * This is also the plug-in class for the model plug-in.
 */
public class AdventureBuilderModel extends Plugin {
	public static final String PLUGIN_ID = "org.eclipse.ui.examples.rcp.adventurebuilder.model";
	private static final String MODEL_TRACKER = "modelTracker";
	
	public static AdventureBuilderModel singleton;

	private ICatalogService catalogService;

	private AppModel appModel;

	public static AdventureBuilderModel getDefault() {
		return singleton;
	}

	public AdventureBuilderModel() {
		singleton = this;
	}

	public ICatalogService getCatalogService() {
		return (ICatalogService) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return getCatalogService0();
			}
		});
	}

	private ICatalogService getCatalogService0() {
		if (catalogService == null) {
			catalogService = new CachingCatalogService(new WebCatalogService());
		}
		return catalogService;
	}

	public AppModel getAppModel() {
		if (appModel == null) {
			appModel = AdventureFactory.eINSTANCE.createAppModel();
		}
		return appModel;
	}
	
	public GlobalSettings getGlobalSettings() {
		AppModel appModel = getAppModel();
		if (appModel.getGlobalSettings() == null) {
			appModel.setGlobalSettings(AdventureFactory.eINSTANCE.createGlobalSettings());
		}
		return appModel.getGlobalSettings();
	}

	/**
	 * Returns the catalog.  The catalog service is queried only the first time.
	 * 
	 * @return the catalog
	 */
	public Catalog getCatalog() {
		AppModel appModel = getAppModel();
		if (appModel.getCatalog() == null) {
			appModel.setCatalog(getCatalogService().getCatalog());
		}
		return appModel.getCatalog();
	}

	public void clearCatalog() {
		getAppModel().setCatalog(null);
	}
	
	public String getServerAddress() {
		return getPluginPreferences().getString(
				IPreferenceConstants.P_SERVER_ADDRESS);
	}

	public void checkout(Order order) {
			if (System.getSecurityManager() != null)
				 System.getSecurityManager().checkPermission(new CheckoutPermission("checkout"));
			
			PurchaseOrder purchaseOrder = PurchaseOrderUtil.convertToPurchaseOrder(order);
			try {
				order.setOrderId(getPurchaseOrderIntf().submitPurchaseOrder(purchaseOrder));
				getAppModel().getOrders().add(order);
				return;
			} catch (ProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidPOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return;
	}

	/**
	 * Returns the Purchase Order service interface at the server address
	 * specified in the preferences.
	 * 
	 * @return the purchase order service interface
	 * @throws ServiceException
	 */
	private PurchaseOrderIntf getPurchaseOrderIntf() throws ServiceException {
		java.net.URL endpoint;
		try {
			String serverAddress = AdventureBuilderModel.getDefault()
					.getServerAddress();
			endpoint = new java.net.URL(serverAddress
					+ "/webservice/PoEndpointBean");
		} catch (java.net.MalformedURLException e) {
			throw new javax.xml.rpc.ServiceException(e);
		}
		return new OpcPurchaseOrderServiceLocator()
				.getPurchaseOrderIntfPort(endpoint);
	}
	
	/**
	 * Clears the cached catalog, if any.
	 */
	public void clearCachedCatalog() {
		((CachingCatalogService) getCatalogService()).clearCache();
	}

	private IRegistryChangeListener listener;
	
	private void registerRegistryListener() {
		listener = new IRegistryChangeListener() {
			public void registryChanged(IRegistryChangeEvent event) {
				IExtensionDelta[] deltas = event.getExtensionDeltas(PLUGIN_ID, MODEL_TRACKER);
				for (int i = 0; i < deltas.length; i++) {
					if (deltas[i].getKind() == IExtensionDelta.ADDED)
						processExtension(deltas[i].getExtension().getConfigurationElements());
					//TODO Need to handle the case of removal, or see if we can use an ExtensionTracker
				}
			}
		};
		Platform.getExtensionRegistry().addRegistryChangeListener(listener, PLUGIN_ID);
		
	}
	public void start(BundleContext context) throws Exception {
		super.start(context);
		registerRegistryListener();
		processExistingExtensions();
	}
	
	public void stop(BundleContext context) throws Exception {
		Platform.getExtensionRegistry().removeRegistryChangeListener(listener);
		//TODO Need to remove the listener from the emf model as well
	}
	
	private void processExistingExtensions() {
		IExtensionPoint xpt = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, MODEL_TRACKER);
		processExtension(xpt.getConfigurationElements());
	}

	private void processExtension(IConfigurationElement[] ces) {
		for (int i = 0; i < ces.length; i++) {
			AdapterImpl o;
			try {
				o = (AdapterImpl) ces[i].createExecutableExtension("tracker");
				getAppModel().eAdapters().add(o);
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.