components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/MimeTypes.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) 2010, 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.util.*;
       
    29 import javax.swing.Icon;
       
    30 import com.oracle.solaris.scf.common.ScfException;
       
    31 import com.oracle.solaris.vp.panel.common.model.*;
       
    32 import com.oracle.solaris.vp.panel.common.smf.*;
       
    33 import com.oracle.solaris.vp.panel.common.view.PanelIconUtil;
       
    34 import com.oracle.solaris.vp.util.misc.*;
       
    35 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    36 import com.oracle.solaris.vp.util.misc.property.*;
       
    37 import com.oracle.solaris.vp.util.swing.HasIcon;
       
    38 
       
    39 @SuppressWarnings({"serial"})
       
    40 public class MimeTypes extends AbstractManagedObject<MimeType>
       
    41     implements HasIcon {
       
    42 
       
    43     //
       
    44     // Static data
       
    45     //
       
    46 
       
    47     public static final String ID = "mimetypes";
       
    48     public static final String NAME = Finder.getString("mimetypes.title");
       
    49     public static final String PROPERTY_ADDED = "added MIME types";
       
    50     public static final String PROPERTY_REMOVED = "removed MIME types";
       
    51 
       
    52     //
       
    53     // Instance data
       
    54     //
       
    55 
       
    56     private ApacheInfo info;
       
    57     private SimpleHasId tmpHasId = new SimpleHasId();
       
    58 
       
    59     private BasicMutableProperty<Integer> addedProperty =
       
    60 	new IntegerProperty(PROPERTY_ADDED);
       
    61 
       
    62     private BasicMutableProperty<Integer> removedProperty =
       
    63 	new IntegerProperty(PROPERTY_REMOVED);
       
    64 
       
    65     private List<MimeType> removed = new ArrayList<MimeType>();
       
    66 
       
    67     //
       
    68     // Constructors
       
    69     //
       
    70 
       
    71     public MimeTypes(ApacheInfo info) {
       
    72 	super(ID);
       
    73 	this.info = info;
       
    74 
       
    75 	addedProperty.update(0, true);
       
    76 	addProperties(addedProperty);
       
    77 
       
    78 	removedProperty.update(0, true);
       
    79 	addProperties(removedProperty);
       
    80 
       
    81 	setComparator(SimpleHasId.COMPARATOR);
       
    82     }
       
    83 
       
    84     //
       
    85     // HasIcon methods
       
    86     //
       
    87 
       
    88     @Override
       
    89     public Icon getIcon(int height) {
       
    90 	return PanelIconUtil.getClosestMimeIcon(null, null, height);
       
    91     }
       
    92 
       
    93     //
       
    94     // ManagedObject methods
       
    95     //
       
    96 
       
    97     @Override
       
    98     public String getName() {
       
    99 	return NAME;
       
   100     }
       
   101 
       
   102     //
       
   103     // AbstractManagedObject methods
       
   104     //
       
   105 
       
   106     @Override
       
   107     public void addChildren(MimeType... toAdd) {
       
   108 	super.addChildren(toAdd);
       
   109 
       
   110 	ChangeableAggregator aggregator = getChangeableAggregator();
       
   111 	for (MimeType mimeType : toAdd) {
       
   112 	    if (!mimeType.isInRepo()) {
       
   113 		addedProperty.setValue(addedProperty.getValue() + 1);
       
   114 	    }
       
   115 	    aggregator.addChangeables(mimeType.getChangeableAggregator());
       
   116 	}
       
   117     }
       
   118 
       
   119     @Override
       
   120     public void removeChildren(MimeType... toRemove) {
       
   121 	super.removeChildren(toRemove);
       
   122 
       
   123 	ChangeableAggregator aggregator = getChangeableAggregator();
       
   124 	for (MimeType mimeType : toRemove) {
       
   125 	    if (!mimeType.isInRepo()) {
       
   126 		addedProperty.setValue(addedProperty.getValue() - 1);
       
   127 	    }
       
   128 	    aggregator.removeChangeable(mimeType.getChangeableAggregator());
       
   129 	}
       
   130     }
       
   131 
       
   132     //
       
   133     // MimeTypes methods
       
   134     //
       
   135 
       
   136     public ApacheInfo getApacheInfo() {
       
   137 	return info;
       
   138     }
       
   139 
       
   140     public MimeType getMimeType(String id) {
       
   141 	synchronized (children) {
       
   142 	    tmpHasId.setId(id);
       
   143 	    int index = Collections.binarySearch(
       
   144 		getChildren(), tmpHasId, SimpleHasId.COMPARATOR);
       
   145 
       
   146 	    if (index >= 0) {
       
   147 		return children.get(index);
       
   148 	    }
       
   149 
       
   150 	    return null;
       
   151 	}
       
   152     }
       
   153 
       
   154     public MimeType getMimeType(String type, String subtype) {
       
   155 	String id = MimeType.toId(type, subtype);
       
   156 	return getMimeType(id);
       
   157     }
       
   158 
       
   159     public void refresh(boolean force) throws ScfException {
       
   160 	ServiceMXBean service = info.getPanelDescriptor().getService();
       
   161 	String group = info.getPropertyGroupName();
       
   162 
       
   163 	// Build list of names of MIME type properties
       
   164 	List<String> properties = new ArrayList<String>();
       
   165 	for (String property : service.getPropertyNames(group)) {
       
   166 	    if (property.startsWith(MimeType.SMF_PROPERTY_PREFIX)) {
       
   167 		properties.add(property);
       
   168 	    }
       
   169 	}
       
   170 	Collections.sort(properties);
       
   171 
       
   172 	// Remove names of properties that are pending removal on client
       
   173 	for (Iterator<MimeType> i = removed.iterator(); i.hasNext();) {
       
   174 	    MimeType mimeType = i.next();
       
   175 	    String property = mimeType.getPropertyName();
       
   176 	    if (properties.remove(property)) {
       
   177 		// Pending removal on client, still exists on server
       
   178 	    } else {
       
   179 		// Pending removal on client, but already removed on server
       
   180 		i.remove();
       
   181 		int n = removed.size();
       
   182 		assert n == removedProperty.getValue() - 1;
       
   183 		removedProperty.setValue(n);
       
   184 	    }
       
   185 	}
       
   186 
       
   187 	// Remove names of properties already created on the client
       
   188 	List<MimeType> children = new ArrayList<MimeType>(getChildren());
       
   189 	for (MimeType mimeType : children) {
       
   190 	    if (mimeType.isInRepo()) {
       
   191 		String property = mimeType.getPropertyName();
       
   192 		if (properties.remove(property)) {
       
   193 		    // Exists on server, already exists on client
       
   194 		    mimeType.refresh(force);
       
   195 		} else {
       
   196 		    // Deleted on server, now must be deleted on client
       
   197 		    removeChildren(mimeType);
       
   198 		}
       
   199 	    }
       
   200 	}
       
   201 
       
   202 	// The list now contains only properties that have been created on the
       
   203 	// server but don't yet exist on the client
       
   204 	for (String property : properties) {
       
   205 	    MimeType mimeType = new MimeType(this, property);
       
   206 	    addChildren(mimeType);
       
   207 	}
       
   208     }
       
   209 
       
   210     public void scheduleRemove(MimeType... mimeTypes) {
       
   211 	for (MimeType mimeType : mimeTypes) {
       
   212 	    if (getChildren().contains(mimeType)) {
       
   213 		removeChildren(mimeType);
       
   214 
       
   215 		if (mimeType.isInRepo()) {
       
   216 		    removed.add(mimeType);
       
   217 		    int n = removed.size();
       
   218 		    assert n == removedProperty.getValue() + 1;
       
   219 		    removedProperty.setValue(n);
       
   220 		}
       
   221 	    }
       
   222 	}
       
   223     }
       
   224 
       
   225     public void saveToRepo() throws ScfException {
       
   226 	ApacheUtil.saveToRepo(info.getService(),
       
   227 	    new ScfRunnable() {
       
   228 		@Override
       
   229 		public void run() throws ScfException {
       
   230 		    for (MimeType mimeType : getChildren()) {
       
   231 			mimeType.saveToRepo();
       
   232 		    }
       
   233 		    addedProperty.setValue(0);
       
   234 
       
   235 		    for (Iterator<MimeType> i = removed.iterator();
       
   236 			i.hasNext();) {
       
   237 
       
   238 			MimeType mimeType = i.next();
       
   239 			mimeType.removeFromRepo();
       
   240 			i.remove();
       
   241 			int n = removed.size();
       
   242 			assert n == removedProperty.getValue() - 1;
       
   243 			removedProperty.setValue(n);
       
   244 		    }
       
   245 
       
   246 		    for (MutableProperty<?> property : getProperties()) {
       
   247 			if (property.isChanged()) {
       
   248 			    if (property instanceof SmfMutableProperty) {
       
   249 				((SmfMutableProperty)property).saveToRepo();
       
   250 			    } else {
       
   251 				property.save();
       
   252 			    }
       
   253 			}
       
   254 		    }
       
   255 		}
       
   256 	    });
       
   257     }
       
   258 }