components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/ApachePanelDescriptor.java
changeset 827 0944d8c0158b
child 1410 ca9946e5736c
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.apache.client.swing;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.util.*;
       
    30 import javax.management.InstanceNotFoundException;
       
    31 import javax.swing.*;
       
    32 import javax.swing.filechooser.FileSystemView;
       
    33 import com.oracle.solaris.adr.Stability;
       
    34 import com.oracle.solaris.scf.common.ScfException;
       
    35 import com.oracle.solaris.vp.panel.common.*;
       
    36 import com.oracle.solaris.vp.panel.common.api.network.*;
       
    37 import com.oracle.solaris.vp.panel.common.api.file.RemoteFileSystemView;
       
    38 import com.oracle.solaris.vp.panel.common.api.smf_old.PropertyGroup;
       
    39 import com.oracle.solaris.vp.panel.common.control.*;
       
    40 import com.oracle.solaris.vp.panel.common.model.*;
       
    41 import com.oracle.solaris.vp.panel.common.smf.*;
       
    42 import com.oracle.solaris.vp.panel.swing.control.PanelFrameControl;
       
    43 import com.oracle.solaris.vp.panel.swing.model.SwingPanelDescriptor;
       
    44 import com.oracle.solaris.vp.util.misc.*;
       
    45 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    46 import com.oracle.solaris.vp.util.misc.property.*;
       
    47 import com.oracle.solaris.vp.util.swing.HasIcons;
       
    48 
       
    49 @SuppressWarnings({"serial"})
       
    50 public class ApachePanelDescriptor
       
    51     extends ServicePanelDescriptor<VirtualHost>
       
    52     implements SwingPanelDescriptor<VirtualHost>, ApacheInfo, HasIcons {
       
    53 
       
    54     //
       
    55     // Static data
       
    56     //
       
    57 
       
    58     // XXX More sizes should be provided
       
    59     protected static final List<ImageIcon> icons = Finder.getIcons(
       
    60 	"images/apache-32.png");
       
    61 
       
    62     public static final String SMF_GROUP = "httpd";
       
    63 
       
    64     public static final String PROPERTY_CUSTOM_ENABLED = "custom_conf";
       
    65     public static final String PROPERTY_CUSTOM_FILE = "custom_file";
       
    66     public static final String PROPERTY_ADDED = "added vhosts";
       
    67     public static final String PROPERTY_REMOVED = "removed vhosts";
       
    68 
       
    69     private static final String SERVICE = "network/vpanels-http";
       
    70     private static final String INSTANCE = "apache2";
       
    71 
       
    72     // Sort based on domain, then ID
       
    73     private static final Comparator<VirtualHost> COMPARATOR =
       
    74 	new Comparator<VirtualHost>() {
       
    75 	    @Override
       
    76 	    public int compare(VirtualHost o1, VirtualHost o2) {
       
    77 		int result = ObjectUtil.compare(
       
    78 		    o1.getDomainProperty().getFirstValue(),
       
    79 		    o2.getDomainProperty().getFirstValue());
       
    80 
       
    81 		if (result != 0) {
       
    82 		    return result;
       
    83 		}
       
    84 
       
    85 		return ObjectUtil.compare(o1.getId(), o2.getId());
       
    86 	    }
       
    87 	};
       
    88 
       
    89     //
       
    90     // Instance data
       
    91     //
       
    92 
       
    93     private DefaultControl control;
       
    94     private RemoteFileSystemView fsView;
       
    95     private MXBeanTracker<NetworkMXBean> networkBeanTracker;
       
    96 
       
    97     private BasicSmfMutableProperty<Boolean> customEnabledProperty =
       
    98 	new BooleanSmfProperty(PROPERTY_CUSTOM_ENABLED, this);
       
    99 
       
   100     private BasicSmfMutableProperty<String> customFileProperty =
       
   101 	new StringSmfProperty(PROPERTY_CUSTOM_FILE, this);
       
   102 
       
   103     private BasicMutableProperty<Integer> addedProperty =
       
   104 	new IntegerProperty(PROPERTY_ADDED);
       
   105 
       
   106     private BasicMutableProperty<Integer> removedProperty =
       
   107 	new IntegerProperty(PROPERTY_REMOVED);
       
   108 
       
   109     private MimeTypes mimeTypes;
       
   110     private Modules modules;
       
   111     private PropertyGroupNamePool vHostNamePool;
       
   112     private List<VirtualHost> removed = new ArrayList<VirtualHost>();
       
   113 
       
   114     //
       
   115     // Constructors
       
   116     //
       
   117 
       
   118     public ApachePanelDescriptor(String id, ClientContext context)
       
   119 	throws IOException, ScfException, InvalidScfDataException,
       
   120 	MissingScfDataException, InstanceNotFoundException,
       
   121 	TrackerException {
       
   122 
       
   123 	super(id, context, SERVICE, INSTANCE);
       
   124 
       
   125 	fsView = new RemoteFileSystemView(context);
       
   126 
       
   127 	networkBeanTracker = new MXBeanTracker<NetworkMXBean>(
       
   128 	    NetworkUtil.OBJECT_NAME, NetworkMXBean.class, Stability.PRIVATE,
       
   129 	    context);
       
   130 
       
   131 	vHostNamePool = new PropertyGroupNamePool(getService(),
       
   132 	    VirtualHost.SMF_GROUP_PREFIX);
       
   133 
       
   134 	customEnabledProperty.setFirstValue(false);
       
   135 	addProperties(customEnabledProperty);
       
   136 
       
   137 	customFileProperty.setFirstValue("");
       
   138 	addProperties(customFileProperty);
       
   139 
       
   140 	addProperties(getEnabledProperty());
       
   141 
       
   142 	addedProperty.update(0, true);
       
   143 	addProperties(addedProperty);
       
   144 
       
   145 	removedProperty.update(0, true);
       
   146 	addProperties(removedProperty);
       
   147 
       
   148 	setComparator(COMPARATOR);
       
   149 
       
   150 	ChangeableAggregator aggregator = getChangeableAggregator();
       
   151 
       
   152 	mimeTypes = new MimeTypes(this);
       
   153 	aggregator.addChangeables(mimeTypes.getChangeableAggregator());
       
   154 
       
   155 	modules = new Modules(this);
       
   156 	aggregator.addChangeables(modules.getChangeableAggregator());
       
   157 
       
   158 	refresh(true);
       
   159 	control = new PanelFrameControl<ApachePanelDescriptor>(this);
       
   160 	control.addChildren(new MainControl(this));
       
   161     }
       
   162 
       
   163     //
       
   164     // HasIcons methods
       
   165     //
       
   166 
       
   167     @Override
       
   168     public List<? extends Icon> getIcons() {
       
   169 	return icons;
       
   170     }
       
   171 
       
   172     //
       
   173     // SmfPropertyGroupInfo methods
       
   174     //
       
   175 
       
   176     @Override
       
   177     public String getPropertyGroupName() {
       
   178 	return SMF_GROUP;
       
   179     }
       
   180 
       
   181     //
       
   182     // SmfPropertyInfo methods
       
   183     //
       
   184 
       
   185     @Override
       
   186     public String getPropertyName() {
       
   187 	// Use the default property names
       
   188 	return null;
       
   189     }
       
   190 
       
   191     //
       
   192     // ApacheInfo methods
       
   193     //
       
   194 
       
   195     @Override
       
   196     public ApachePanelDescriptor getPanelDescriptor() {
       
   197 	return this;
       
   198     }
       
   199 
       
   200     //
       
   201     // ManagedObject methods
       
   202     //
       
   203 
       
   204     @Override
       
   205     public void dispose() {
       
   206 	super.dispose();
       
   207 	networkBeanTracker.dispose();
       
   208 	fsView.dispose();
       
   209     }
       
   210 
       
   211     //
       
   212     // PanelDescriptor methods
       
   213     //
       
   214 
       
   215     @Override
       
   216     public Control getControl() {
       
   217 	return control;
       
   218     }
       
   219 
       
   220     //
       
   221     // AbstractManagedObject methods
       
   222     //
       
   223 
       
   224     @Override
       
   225     public void addChildren(VirtualHost... toAdd) {
       
   226 	super.addChildren(toAdd);
       
   227 
       
   228 	ChangeableAggregator aggregator = getChangeableAggregator();
       
   229 	for (VirtualHost vHost : toAdd) {
       
   230 	    if (!vHost.isInRepo()) {
       
   231 		addedProperty.setValue(addedProperty.getValue() + 1);
       
   232 	    }
       
   233 	    aggregator.addChangeables(vHost.getChangeableAggregator());
       
   234 	}
       
   235     }
       
   236 
       
   237     @Override
       
   238     public void removeChildren(VirtualHost... toRemove) {
       
   239 	super.removeChildren(toRemove);
       
   240 
       
   241 	ChangeableAggregator aggregator = getChangeableAggregator();
       
   242 	for (VirtualHost vHost : toRemove) {
       
   243 	    if (!vHost.isInRepo()) {
       
   244 		addedProperty.setValue(addedProperty.getValue() - 1);
       
   245 	    }
       
   246 	    aggregator.removeChangeable(vHost.getChangeableAggregator());
       
   247 	}
       
   248     }
       
   249 
       
   250     //
       
   251     // ServicePanelDescriptor methods
       
   252     //
       
   253 
       
   254     @Override
       
   255     public void refresh(boolean force) throws ScfException,
       
   256 	InvalidScfDataException, MissingScfDataException {
       
   257 
       
   258 	super.refresh(force);
       
   259 
       
   260 	for (MutableProperty<?> property : getProperties()) {
       
   261 	    if (property instanceof SmfMutableProperty) {
       
   262 		((SmfMutableProperty)property).updateFromRepo(force);
       
   263 	    }
       
   264 	}
       
   265 
       
   266 	// Build list of names of virtual host property groups
       
   267 	List<String> groups = new ArrayList<String>();
       
   268 	for (PropertyGroup pg : getService().getPropertyGroups()) {
       
   269 	    String group = pg.getName();
       
   270 	    if (group.startsWith(VirtualHost.SMF_GROUP_PREFIX)) {
       
   271 		groups.add(group);
       
   272 	    }
       
   273 	}
       
   274 	Collections.sort(groups);
       
   275 
       
   276 	// Remove names of groups that are pending removal on client
       
   277 	for (Iterator<VirtualHost> i = removed.iterator(); i.hasNext(); ) {
       
   278 	    VirtualHost vHost = i.next();
       
   279 	    String group = vHost.getPropertyGroupName();
       
   280 	    if (groups.remove(group)) {
       
   281 		// Pending removal on client, still exists on server
       
   282 	    } else {
       
   283 		// Pending removal on client, but already removed on server
       
   284 		i.remove();
       
   285 		int n = removed.size();
       
   286 		assert n == removedProperty.getValue() - 1;
       
   287 		removedProperty.setValue(n);
       
   288 	    }
       
   289 	}
       
   290 
       
   291 	// Remove names of groups already created on the client
       
   292 	List<VirtualHost> children = new ArrayList<VirtualHost>(getChildren());
       
   293 	for (VirtualHost vHost : children) {
       
   294 	    if (vHost.isInRepo()) {
       
   295 		String group = vHost.getPropertyGroupName();
       
   296 		if (groups.remove(group)) {
       
   297 		    // Exists on server, already exists on client
       
   298 		    vHost.refresh(force);
       
   299 		} else {
       
   300 		    // Deleted on server, now must be deleted on client
       
   301 		    removeChildren(vHost);
       
   302 		}
       
   303 	    }
       
   304 	}
       
   305 
       
   306 	// The list now contains only groups that have been created on the
       
   307 	// server but don't yet exist on the client
       
   308 	for (String group : groups) {
       
   309 	    VirtualHost vHost = new VirtualHost(this, group);
       
   310 	    addChildren(vHost);
       
   311 	}
       
   312 
       
   313 	mimeTypes.refresh(force);
       
   314 	modules.refresh(force);
       
   315     }
       
   316 
       
   317     //
       
   318     // ApachePanelDescriptor methods
       
   319     //
       
   320 
       
   321     public BasicSmfMutableProperty<Boolean> getCustomEnabledProperty() {
       
   322 	return customEnabledProperty;
       
   323     }
       
   324 
       
   325     public BasicSmfMutableProperty<String> getCustomFileProperty() {
       
   326 	return customFileProperty;
       
   327     }
       
   328 
       
   329     public FileSystemView getFileSystemView() {
       
   330 	return fsView;
       
   331     }
       
   332 
       
   333     public MimeTypes getMimeTypes() {
       
   334 	return mimeTypes;
       
   335     }
       
   336 
       
   337     public Modules getModules() {
       
   338 	return modules;
       
   339     }
       
   340 
       
   341     public VirtualHost getVirtualHost(String id) {
       
   342 	synchronized (children) {
       
   343 	    for (VirtualHost vHost : children) {
       
   344 		if (vHost.getId().equals(id)) {
       
   345 		    return vHost;
       
   346 		}
       
   347 	    }
       
   348 	    return null;
       
   349 	}
       
   350     }
       
   351 
       
   352     /**
       
   353      * Gets the first virtual host with the given domain.
       
   354      */
       
   355     public VirtualHost getVirtualHostByDomain(String domain) {
       
   356 	synchronized (children) {
       
   357 	    for (VirtualHost vHost : children) {
       
   358 		if (vHost.getDomainProperty().getFirstValue().equals(domain)) {
       
   359 		    return vHost;
       
   360 		}
       
   361 	    }
       
   362 	    return null;
       
   363 	}
       
   364     }
       
   365 
       
   366     protected PropertyGroupNamePool getVirtualHostNamePool() {
       
   367 	return vHostNamePool;
       
   368     }
       
   369 
       
   370     public void scheduleRemove(VirtualHost... vHosts) {
       
   371 	for (VirtualHost vHost : vHosts) {
       
   372 	    if (getChildren().contains(vHost)) {
       
   373 		removeChildren(vHost);
       
   374 
       
   375 		if (vHost.isInRepo()) {
       
   376 		    removed.add(vHost);
       
   377 		    int n = removed.size();
       
   378 		    assert n == removedProperty.getValue() + 1;
       
   379 		    removedProperty.setValue(n);
       
   380 		}
       
   381 	    }
       
   382 	}
       
   383     }
       
   384 
       
   385     public void saveToRepo() throws ScfException {
       
   386 	ApacheUtil.saveToRepo(getService(),
       
   387 	    new ScfRunnable() {
       
   388 		@Override
       
   389 		public void run() throws ScfException {
       
   390 		    for (VirtualHost vHost : getChildren()) {
       
   391 			vHost.saveToRepo();
       
   392 		    }
       
   393 		    addedProperty.setValue(0);
       
   394 
       
   395 		    for (Iterator<VirtualHost> i = removed.iterator();
       
   396 			i.hasNext(); ) {
       
   397 
       
   398 			VirtualHost vHost = i.next();
       
   399 			vHost.removeFromRepo();
       
   400 			i.remove();
       
   401 			int n = removed.size();
       
   402 			assert n == removedProperty.getValue() - 1;
       
   403 			removedProperty.setValue(n);
       
   404 		    }
       
   405 
       
   406 		    for (MutableProperty<?> property : getProperties()) {
       
   407 			if (property.isChanged()) {
       
   408 			    if (property instanceof SmfMutableProperty) {
       
   409 				((SmfMutableProperty)property).saveToRepo();
       
   410 			    } else {
       
   411 				property.save();
       
   412 			    }
       
   413 			}
       
   414 		    }
       
   415 
       
   416 		    mimeTypes.saveToRepo();
       
   417 		    modules.saveToRepo();
       
   418 		}
       
   419 	    });
       
   420     }
       
   421 
       
   422     protected void validate() throws GlobalValidationException,
       
   423         VirtualHostGeneralValidationException,
       
   424 	VirtualHostSSLValidationException {
       
   425 
       
   426 	validateCustomFile();
       
   427 	for (VirtualHost vHost : getChildren()) {
       
   428 	    vHost.validate();
       
   429 	}
       
   430     }
       
   431 
       
   432     //
       
   433     // Private methods
       
   434     //
       
   435 
       
   436     private void validateCustomFile()
       
   437 	throws GlobalValidationException {
       
   438 
       
   439 	if (customEnabledProperty.getFirstValue() &&
       
   440 	   (customEnabledProperty.isChanged() ||
       
   441 	    customFileProperty.isChanged()) &&
       
   442 	    customFileProperty.getFirstValue().isEmpty()) {
       
   443 	    throw new GlobalValidationException(
       
   444 		Finder.getString("global.error.customfile.empty"));
       
   445 	}
       
   446     }
       
   447 
       
   448     //
       
   449     // ApachePanelDescriptor classes
       
   450     //
       
   451 
       
   452     public NetworkMXBean getNetworkMXBean() {
       
   453         return networkBeanTracker.getBean();
       
   454     }
       
   455 }