| /******************************************************************************* |
| * Copyright (c) 2012 Rushan R. Gilmullin and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Rushan R. Gilmullin - initial API and implementation |
| * Florian Pirchner - adjustings for osbp implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.osbp.vaaclipse.addons.app; |
| |
| import java.awt.Component; |
| import java.awt.Container; |
| import java.awt.Dimension; |
| import java.awt.Toolkit; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| import java.net.InetAddress; |
| import java.net.UnknownHostException; |
| import java.util.concurrent.ArrayBlockingQueue; |
| |
| import javax.swing.Box; |
| import javax.swing.BoxLayout; |
| import javax.swing.JButton; |
| import javax.swing.JFrame; |
| import javax.swing.JLabel; |
| import javax.swing.JOptionPane; |
| |
| import org.eclipse.e4.core.services.log.Logger; |
| import org.eclipse.e4.ui.internal.workbench.WorkbenchLogger; |
| import org.eclipse.equinox.app.IApplication; |
| import org.eclipse.equinox.app.IApplicationContext; |
| import org.eclipse.osgi.service.datalocation.Location; |
| import org.eclipse.osbp.vaaclipse.addons.app.webapp.VaadinWebApplication; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.ServiceReference; |
| import org.osgi.service.http.HttpService; |
| import org.eclipse.osbp.vaaclipse.api.ResourceInfoProvider; |
| |
| import com.vaadin.ui.themes.Reindeer; |
| |
| /** |
| * The Class VaadinE4Application. |
| */ |
| @SuppressWarnings("restriction") |
| public class VaadinE4Application implements IApplication, ResourceInfoProvider { |
| |
| /** The queue. */ |
| private ArrayBlockingQueue<String> queue; |
| |
| /** The logger. */ |
| private Logger logger = new WorkbenchLogger( |
| "org.eclipse.osbp.vaaclipse.app"); |
| |
| /** The app context. */ |
| private IApplicationContext appContext; |
| |
| /** The instance. */ |
| private static VaadinE4Application instance; |
| |
| /** The web application. */ |
| private VaadinWebApplication webApplication; |
| |
| /** The Constant EXIT. */ |
| public static final String EXIT = "EXIT"; |
| |
| /** The frame. */ |
| JFrame frame; |
| |
| /** The context path. */ |
| private String contextPath = "/"; |
| |
| /** The app widgetset. */ |
| private String appWidgetset; |
| |
| /** The app auth provider. */ |
| private String appAuthProvider; |
| |
| /** |
| * Gets the single instance of VaadinE4Application. |
| * |
| * @return single instance of VaadinE4Application |
| */ |
| public static VaadinE4Application getInstance() { |
| return instance; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.ResourceInfoProvider#getCssTheme() |
| */ |
| @Override |
| public String getCssTheme() { |
| return webApplication.getThemeId(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.ResourceInfoProvider#getApplicationtWidgetset() |
| */ |
| @Override |
| public String getApplicationtWidgetset() { |
| return appWidgetset; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.ResourceInfoProvider#getApplicationtWidgetsetName() |
| */ |
| @Override |
| public String getApplicationtWidgetsetName() { |
| return webApplication.getWidgetsetName(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.ResourceInfoProvider#getApplicationHeaderIcon() |
| */ |
| @Override |
| public String getApplicationHeaderIcon() { |
| String uri = webApplication.getHeaderIconURI(); |
| if (uri == null) |
| uri = "org.eclipse.osbp.vaaclipse.resources/VAADIN/themes/vaaclipse_default_theme/img/vaaclipse.png"; |
| return uri; |
| } |
| |
| /** |
| * Gets the application authentication provider. |
| * |
| * @return the application authentication provider |
| */ |
| public String getApplicationAuthenticationProvider() { |
| return appAuthProvider; |
| } |
| |
| /** |
| * Gets the instance location. |
| * |
| * @return the instance location |
| */ |
| public Location getInstanceLocation() { |
| return Activator.getDefault().getInstanceLocation(); |
| } |
| |
| /** |
| * Gets the app context. |
| * |
| * @return the app context |
| */ |
| public IApplicationContext getAppContext() { |
| return appContext; |
| } |
| |
| /** |
| * Gets the logger. |
| * |
| * @return the logger |
| */ |
| public Logger getLogger() { |
| return logger; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext) |
| */ |
| @Override |
| public Object start(IApplicationContext context) throws Exception { |
| instance = this; |
| appContext = context; |
| |
| registerServices(); |
| |
| logger.debug("VaadinE4Application.start()"); |
| context.applicationRunning(); |
| |
| queue = new ArrayBlockingQueue<String>(10); |
| |
| startVaadinWebApplication(); |
| |
| String showFrame = appContext.getBrandingProperty("showFrame"); |
| |
| if ("true".equalsIgnoreCase(showFrame)) |
| showFrame(); |
| |
| String msg; |
| while (!(msg = queue.take()).equals(EXIT)) { |
| System.out.println("Vaaclipse Application shutdowned"); |
| } |
| |
| if (frame != null) { |
| frame.setVisible(false); |
| frame.dispose(); |
| } |
| |
| return EXIT_OK; |
| } |
| |
| /** |
| * Register services. |
| */ |
| private void registerServices() { |
| Activator |
| .getDefault() |
| .getBundle() |
| .getBundleContext() |
| .registerService(ResourceInfoProvider.class.getName(), this, |
| null); |
| } |
| |
| /** |
| * Read path property. |
| * |
| * @param propName |
| * the prop name |
| * @return the string |
| */ |
| private String readPathProperty(String propName) { |
| String propValue = appContext.getBrandingProperty(propName); |
| if (propValue == null) |
| return propValue; |
| propValue = propValue.trim(); |
| if (!propValue.startsWith("platform:/plugin/")) { |
| propValue = "platform:/plugin/" + propValue; |
| } |
| return propValue; |
| } |
| |
| /** |
| * Read class property. |
| * |
| * @param propName |
| * the prop name |
| * @return the string |
| */ |
| private String readClassProperty(String propName) { |
| String propValue = appContext.getBrandingProperty(propName); |
| if (propValue == null) |
| return propValue; |
| propValue = propValue.trim(); |
| String pathStart = "bundleclass://"; |
| if (!propValue.startsWith(pathStart)) { |
| propValue = pathStart + propValue; |
| } |
| return propValue; |
| } |
| |
| /** |
| * Start vaadin web application. |
| * |
| * @throws Exception |
| * the exception |
| */ |
| private void startVaadinWebApplication() throws Exception { |
| String port = System.getProperty("org.eclipse.osbp.ecview.jetty.port"); |
| if (port == null) |
| port = "8080"; |
| |
| contextPath = System |
| .getProperty("org.eclipse.osbp.ecview.jetty.contextpath"); |
| |
| if (contextPath == null) |
| contextPath = "/"; |
| |
| String cssTheme = appContext.getBrandingProperty("cssTheme"); |
| |
| if (cssTheme == null) |
| cssTheme = Reindeer.THEME_NAME; |
| |
| appWidgetset = readPathProperty("applicationWidgetset"); |
| if (appWidgetset == null || appWidgetset.trim().isEmpty()) { |
| appWidgetset = "platform:/plugin/org.eclipse.osbp.vaaclipse.widgetset.default/resources/org.eclipse.osbp.vaaclipse.widgetset.DefaultWidgetset"; |
| // appWidgetset = |
| // "platform:/plugin/org.eclipse.osbp.vaaclipse.resources/VAADIN/widgetsets/vaaclipse_widgetset.widgetset.Vaaclipse_widgetsetWidgetset"; |
| } else |
| appWidgetset = appWidgetset.trim(); |
| |
| int index = appWidgetset.lastIndexOf("/"); |
| if (index < 0) |
| throw new IllegalStateException( |
| "applicationWidgetset property has wrong value"); |
| |
| if (index == appWidgetset.length() - 1) { |
| appWidgetset = appWidgetset.substring(0, appWidgetset.length() - 1); |
| index = appWidgetset.lastIndexOf("/"); |
| if (index < 0) |
| throw new IllegalStateException( |
| "applicationWidgetset property has wrong value"); |
| } |
| |
| String appWidgetsetName = appWidgetset.substring(index + 1); |
| |
| String appHeaderIcon = readPathProperty("applicationHeaderIcon"); |
| |
| if (appHeaderIcon == null || appHeaderIcon.trim().isEmpty()) |
| appHeaderIcon = "platform:/plugin/com.vaadin.themes/VAADIN/themes/reindeer/favicon.ico"; |
| |
| appAuthProvider = readClassProperty("applicationAuthenticationProvider"); |
| |
| String productionMode = appContext |
| .getBrandingProperty("vaadin.productionMode"); |
| String disableXsrfProtection = appContext |
| .getBrandingProperty("vaadin.disable-xsrf-protection"); |
| String pushmode = appContext.getBrandingProperty("vaadin.pushmode"); |
| |
| if ("true".equals(disableXsrfProtection)) { |
| System.out.println("Warning: XSRF protection is OFF!"); |
| } |
| |
| final BundleContext bundleContext = Activator.getDefault().getBundle() |
| .getBundleContext(); |
| ServiceReference<?> httpServiceRef = bundleContext |
| .getServiceReference(HttpService.class.getName()); |
| if (httpServiceRef == null) { |
| JOptionPane |
| .showMessageDialog(null, "HttpService is not accessible"); |
| throw new Exception(); |
| } |
| |
| webApplication = new VaadinWebApplication(bundleContext.getBundle()); |
| webApplication.setContextPath(contextPath); |
| webApplication.setWidgetsetName(appWidgetsetName); |
| webApplication.setProductionMode(Boolean |
| .valueOf(productionMode != null ? productionMode : "true")); |
| webApplication |
| .setInitProperty("disable-xsrf-protection", |
| disableXsrfProtection != null ? disableXsrfProtection |
| : "false"); |
| if (pushmode != null) { |
| System.out.println("Push Enabled"); |
| webApplication.setInitProperty("pushmode", pushmode); |
| } |
| webApplication.setPort(Integer.valueOf(port)); |
| webApplication.setHeaderIconURI(appHeaderIcon); |
| webApplication.setThemeId(cssTheme); |
| |
| // start the vaadin application |
| webApplication.activate(); |
| } |
| |
| /** |
| * Show frame. |
| */ |
| private void showFrame() { |
| frame = new JFrame(); |
| frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| frame.setSize(500, 115); |
| frame.setResizable(false); |
| frame.setTitle("Vaaclipse server"); |
| final Container contentPane = frame.getContentPane(); |
| contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); |
| |
| String host; |
| try { |
| InetAddress thisIp = InetAddress.getLocalHost(); |
| host = thisIp.getHostAddress().toString(); |
| } catch (UnknownHostException e1) { |
| host = "localhost"; |
| } |
| |
| final JLabel label = new JLabel(String.format( |
| "Vaaclipse server started at http://%s:%s%s", host, |
| webApplication.getPort(), contextPath)); |
| label.setAlignmentX(Component.CENTER_ALIGNMENT); |
| contentPane.add(label); |
| |
| contentPane.add(Box.createVerticalStrut(5)); |
| |
| final JLabel productionModeLabel = new JLabel("ProductionMode: " |
| + webApplication.isProductionMode()); |
| productionModeLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| contentPane.add(productionModeLabel); |
| |
| contentPane.add(Box.createVerticalStrut(20)); |
| |
| JButton exitButton = new JButton("Shutdown"); |
| exitButton.setAlignmentX(Component.CENTER_ALIGNMENT); |
| exitButton.addActionListener(new ActionListener() { |
| |
| @Override |
| public void actionPerformed(ActionEvent arg0) { |
| shutdown(true); |
| return; |
| } |
| }); |
| contentPane.add(exitButton); |
| |
| frame.addWindowListener(new WindowAdapter() { |
| @Override |
| public void windowClosing(WindowEvent e) { |
| super.windowClosing(e); |
| |
| shutdown(true); |
| return; |
| } |
| }); |
| |
| // centering frame |
| Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize(); |
| int x = (screenDimension.width - frame.getSize().width) / 2; |
| int y = (screenDimension.height - frame.getSize().height) / 2; |
| |
| // Move the window |
| frame.setLocation(x, y); |
| |
| frame.setVisible(true); |
| } |
| |
| /** |
| * Shutdown. |
| * |
| * @param confirm |
| * the confirm |
| * @return true, if successful |
| */ |
| private boolean shutdown(boolean confirm) { |
| boolean exit = true; |
| if (confirm) { |
| exit = JOptionPane.OK_OPTION == JOptionPane.showOptionDialog(frame, |
| "Are you really want shutdown server?", "Warning", |
| JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, |
| null, null, JOptionPane.CANCEL_OPTION); |
| } |
| |
| if (exit) { |
| shutdown(); |
| } |
| return exit; |
| } |
| |
| /** |
| * Shutdown. |
| */ |
| public void shutdown() { |
| System.out.println("Vaaclipse Application will shutdown"); |
| try { |
| queue.put(EXIT); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.equinox.app.IApplication#stop() |
| */ |
| @Override |
| public void stop() { |
| // will never be invoked |
| webApplication.deactivate(); |
| } |
| |
| } |