added access control when applet

This commit is contained in:
fros4943 2008-02-18 08:18:01 +00:00
parent d74fd06536
commit 3675479274
4 changed files with 195 additions and 103 deletions

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: GUI.java,v 1.70 2008/02/12 15:31:22 fros4943 Exp $
* $Id: GUI.java,v 1.71 2008/02/18 08:18:01 fros4943 Exp $
*/
package se.sics.cooja;
@ -36,6 +36,7 @@ import java.beans.PropertyVetoException;
import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlException;
import java.util.*;
import java.util.List;
import javax.swing.*;
@ -88,8 +89,7 @@ public class GUI {
* External tools user settings filename.
*/
public static final String EXTERNAL_TOOLS_USER_SETTINGS_FILENAME = ".cooja.user.properties";
public static File externalToolsUserSettingsFile =
new File(System.getProperty("user.home"), EXTERNAL_TOOLS_USER_SETTINGS_FILENAME);
public static File externalToolsUserSettingsFile;
private static boolean externalToolsUserSettingsFileReadOnly = false;
private static String specifiedContikiPath = null;
@ -102,7 +102,7 @@ public class GUI {
/**
* Default project configuration filename.
*/
public static final String PROJECT_DEFAULT_CONFIG_FILENAME = "/cooja_default.config";
public static String PROJECT_DEFAULT_CONFIG_FILENAME = null;
/**
* User project configuration filename.
@ -258,27 +258,29 @@ public class GUI {
String defaultProjectDirs = getExternalToolsSetting(
"DEFAULT_PROJECTDIRS", null);
if (defaultProjectDirs != null) {
String[] defaultProjectDirsArr = defaultProjectDirs.split(";");
if (defaultProjectDirsArr.length > 0) {
for (String defaultProjectDir : defaultProjectDirsArr) {
File projectDir = new File(defaultProjectDir);
if (projectDir.exists() && projectDir.isDirectory()) {
currentProjectDirs.add(projectDir);
if (!isVisualizedInApplet()) {
String[] defaultProjectDirsArr = defaultProjectDirs.split(";");
if (defaultProjectDirsArr.length > 0) {
for (String defaultProjectDir : defaultProjectDirsArr) {
File projectDir = new File(defaultProjectDir);
if (projectDir.exists() && projectDir.isDirectory()) {
currentProjectDirs.add(projectDir);
}
}
}
}
}
// Load extendable parts (using current project config)
try {
reparseProjectConfig();
} catch (ParseProjectsException e) {
logger.fatal("Error when loading project directories: " + e.getMessage());
e.printStackTrace();
if (myDesktopPane != null) {
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Loading project directories failed.\nStack trace printed to console.",
"Error", JOptionPane.ERROR_MESSAGE);
// Load extendable parts (using current project config)
try {
reparseProjectConfig();
} catch (ParseProjectsException e) {
logger.fatal("Error when loading project directories: " + e.getMessage());
e.printStackTrace();
if (myDesktopPane != null) {
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Loading project directories failed.\nStack trace printed to console.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
@ -423,6 +425,10 @@ public class GUI {
private void updateOpenHistoryMenuItems() {
menuConfOpenSimulation.removeAll();
if (isVisualizedInApplet()) {
return;
}
JMenuItem browseItem = new JMenuItem("Browse...");
browseItem.setActionCommand("confopen sim");
browseItem.addActionListener(guiEventHandler);
@ -502,16 +508,28 @@ public class GUI {
menuOpenSimulation = new JMenu("Open simulation");
menuOpenSimulation.setMnemonic(KeyEvent.VK_O);
menu.add(menuOpenSimulation);
if (isVisualizedInApplet()) {
menuOpenSimulation.setEnabled(false);
menuOpenSimulation.setToolTipText("Not available in applet version");
}
menuConfOpenSimulation = new JMenu("Open & Reconfigure simulation");
menuConfOpenSimulation.setMnemonic(KeyEvent.VK_R);
menu.add(menuConfOpenSimulation);
if (isVisualizedInApplet()) {
menuConfOpenSimulation.setEnabled(false);
menuConfOpenSimulation.setToolTipText("Not available in applet version");
}
menuItem = new JMenuItem("Save simulation");
menuItem.setMnemonic(KeyEvent.VK_S);
menuItem.setActionCommand("save sim");
menuItem.addActionListener(guiEventHandler);
menu.add(menuItem);
if (isVisualizedInApplet()) {
menuItem.setEnabled(false);
menuItem.setToolTipText("Not available in applet version");
}
menu.addSeparator();
@ -529,6 +547,10 @@ public class GUI {
menuItem.setActionCommand("quit");
menuItem.addActionListener(guiEventHandler);
menu.add(menuItem);
if (isVisualizedInApplet()) {
menuItem.setEnabled(false);
menuItem.setToolTipText("Not available in applet version");
}
// Simulation menu
menu = new JMenu("Simulation");
@ -596,6 +618,10 @@ public class GUI {
menuItem.putClientProperty("class", moteTypeClass);
menuItem.setToolTipText(abstractionLevelDescription);
menuItem.addActionListener(guiEventHandler);
if (isVisualizedInApplet() && moteTypeClass.equals(ContikiMoteType.class)) {
menuItem.setEnabled(false);
menuItem.setToolTipText("Not available in applet version");
}
/* Add new item directly after cross level separator */
for (int i=0; i < menuMoteTypeClasses.getMenuComponentCount(); i++) {
@ -683,11 +709,19 @@ public class GUI {
menuItem.setActionCommand("edit paths");
menuItem.addActionListener(guiEventHandler);
menu.add(menuItem);
if (isVisualizedInApplet()) {
menuItem.setEnabled(false);
menuItem.setToolTipText("Not available in applet version");
}
menuItem = new JMenuItem("Manage project directories");
menuItem.setActionCommand("manage projects");
menuItem.addActionListener(guiEventHandler);
menu.add(menuItem);
if (isVisualizedInApplet()) {
menuItem.setEnabled(false);
menuItem.setToolTipText("Not available in applet version");
}
menu.addSeparator();
@ -1384,6 +1418,14 @@ public class GUI {
* Any registered temporary plugins will be saved and reregistered.
*/
public void reparseProjectConfig() throws ParseProjectsException {
if (PROJECT_DEFAULT_CONFIG_FILENAME == null) {
if (isVisualizedInApplet()) {
PROJECT_DEFAULT_CONFIG_FILENAME = "/cooja_applet.config";
} else {
PROJECT_DEFAULT_CONFIG_FILENAME = "/cooja_default.config";
}
}
// Backup temporary plugins
Vector<Class<? extends Plugin>> oldTempPlugins = (Vector<Class<? extends Plugin>>) pluginClassesTemporary
.clone();
@ -1412,32 +1454,34 @@ public class GUI {
+ PROJECT_DEFAULT_CONFIG_FILENAME).initCause(e);
}
// Append project directory configurations
for (File projectDir : currentProjectDirs) {
try {
// Append config to general config
projectConfig.appendProjectDir(projectDir);
} catch (FileNotFoundException e) {
logger.fatal("Could not find project config file: " + projectDir);
throw (ParseProjectsException) new ParseProjectsException(
"Could not find project config file: " + projectDir).initCause(e);
} catch (IOException e) {
logger.fatal("Error when reading project config file: " + projectDir);
throw (ParseProjectsException) new ParseProjectsException(
"Error when reading project config file: " + projectDir).initCause(e);
if (!isVisualizedInApplet()) {
// Append project directory configurations
for (File projectDir : currentProjectDirs) {
try {
// Append config to general config
projectConfig.appendProjectDir(projectDir);
} catch (FileNotFoundException e) {
logger.fatal("Could not find project config file: " + projectDir);
throw (ParseProjectsException) new ParseProjectsException(
"Could not find project config file: " + projectDir).initCause(e);
} catch (IOException e) {
logger.fatal("Error when reading project config file: " + projectDir);
throw (ParseProjectsException) new ParseProjectsException(
"Error when reading project config file: " + projectDir).initCause(e);
}
}
}
// Create class loader
try {
projectDirClassLoader = createClassLoader(currentProjectDirs);
} catch (ClassLoaderCreationException e) {
throw (ParseProjectsException) new ParseProjectsException(
"Error when creating class loader").initCause(e);
// Create class loader
try {
projectDirClassLoader = createClassLoader(currentProjectDirs);
} catch (ClassLoaderCreationException e) {
throw (ParseProjectsException) new ParseProjectsException(
"Error when creating class loader").initCause(e);
}
} else {
projectDirClassLoader = null;
}
// Register mote types
String[] moteTypeClassNames = projectConfig.getStringArrayValue(GUI.class,
"MOTETYPES");
@ -1527,6 +1571,9 @@ public class GUI {
// Register radio mediums
String[] radioMediumsClassNames = projectConfig.getStringArrayValue(
GUI.class, "RADIOMEDIUMS");
for (String s: radioMediumsClassNames) {
System.out.println(">>>: " + s);
}
if (radioMediumsClassNames != null) {
for (String radioMediumClassName : radioMediumsClassNames) {
Class<? extends RadioMedium> radioMediumClass = tryLoadClass(this,
@ -2020,6 +2067,9 @@ public class GUI {
* @param configFile Configuration file to load, if null a dialog will appear
*/
public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile) {
if (isVisualizedInApplet()) {
return;
}
if (CoreComm.hasLibraryBeenLoaded()) {
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
@ -2315,6 +2365,10 @@ public class GUI {
* Ask for confirmation before overwriting file
*/
public void doSaveConfig(boolean askForConfirmation) {
if (isVisualizedInApplet()) {
return;
}
if (mySimulation != null) {
mySimulation.stopSimulation();
@ -2423,6 +2477,10 @@ public class GUI {
* Should we ask for confirmation before quitting?
*/
public void doQuit(boolean askForConfirmation) {
if (isVisualizedInApplet()) {
return;
}
if (askForConfirmation) {
String s1 = "Quit";
String s2 = "Cancel";
@ -2557,6 +2615,10 @@ public class GUI {
* Load user values from external properties file
*/
public static void loadExternalToolsUserSettings() {
if (externalToolsUserSettingsFile == null) {
return;
}
try {
FileInputStream in = new FileInputStream(externalToolsUserSettingsFile);
Properties settings = new Properties();
@ -2581,6 +2643,10 @@ public class GUI {
* Save external tools user settings to file.
*/
public static void saveExternalToolsUserSettings() {
if (isVisualizedInApplet()) {
return;
}
if (externalToolsUserSettingsFileReadOnly) {
return;
}
@ -2739,14 +2805,16 @@ public class GUI {
} catch (UnsupportedClassVersionError e) {
}
try {
if (projectDirClassLoader != null) {
return projectDirClassLoader.loadClass(className).asSubclass(
classType);
if (!isVisualizedInApplet()) {
try {
if (projectDirClassLoader != null) {
return projectDirClassLoader.loadClass(className).asSubclass(
classType);
}
} catch (NoClassDefFoundError e) {
} catch (ClassNotFoundException e) {
} catch (UnsupportedClassVersionError e) {
}
} catch (NoClassDefFoundError e) {
} catch (ClassNotFoundException e) {
} catch (UnsupportedClassVersionError e) {
}
return null;
@ -2877,6 +2945,7 @@ public class GUI {
*/
public static void main(String[] args) {
try {
// Configure logger
if ((new File(LOG_CONFIG_FILE)).exists()) {
DOMConfigurator.configure(LOG_CONFIG_FILE);
@ -2885,6 +2954,11 @@ public class GUI {
DOMConfigurator.configure(GUI.class.getResource("/" + LOG_CONFIG_FILE));
}
externalToolsUserSettingsFile = new File(System.getProperty("user.home"), EXTERNAL_TOOLS_USER_SETTINGS_FILENAME);
} catch (AccessControlException e) {
externalToolsUserSettingsFile = null;
}
// Parse general command arguments
for (String element : args) {
if (element.startsWith("-contiki=")) {
@ -3013,6 +3087,7 @@ public class GUI {
public void run() {
JDesktopPane desktop = new JDesktopPane();
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
applet = CoojaApplet.applet;
GUI gui = new GUI(desktop);
configureApplet(gui, false);
}

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ProjectConfig.java,v 1.1 2007/03/23 23:34:33 fros4943 Exp $
* $Id: ProjectConfig.java,v 1.2 2008/02/18 08:18:18 fros4943 Exp $
*/
package se.sics.cooja;
@ -38,49 +38,49 @@ import org.apache.log4j.Logger;
/**
* A project configuration may hold the configuration for one or several project
* directories as well as a general simulator configuration.
*
*
* The configuration for a project directory may for example consist of which
* plugins, interfaces and processes that the specific project directory supplies.
* Each project directory configuration is read from the property file cooja.config, a
* file which is required in each project directory.
*
*
* Values can be fetched as String, Boolean, Integer, Double or String array.
*
*
* Several configurations can be merged, together forming a final overall
* configuration. The order of the how configurations are merged matter - later
* values will overwrite earlier. For example merging two configurations with
* the key 'SOMEKEY' in the following order:
*
*
* SOMEKEY = a b c
*
*
* SOMEKEY = d e
*
*
* will result in the final value "d e".
*
*
* If a specific value should be extended instead of overwritten, the value must
* start with a single space-surrounded '+'. For example, merging two
* configurations with the key as above in the following order:
*
*
* SOMEKEY = a b c
*
*
* SOMEKEY = + d e
*
*
* will result in the final value "a b c d e".
*
*
* The simulator will hold a merged project configuration, depending on which
* project directories are used. Additionally. each mote type may also have a
* configuration of its own, that differs from the general simulator
* configuration.
*
*
* Often, but not necessarily, keys are named depending on which class is
* associated with the information. For example, let's say a battery interface
* wants to store its initial capacity (a double) using this approach. Data
* stored in the external configuration file can look like the following:
* se.sics.cooja.interfaces.Battery.initial_capacity 54.123321
*
*
* This value is then be read by: myMoteTypeConfig.getDoubleValue(Battery.class,
* "initial_capacity");
*
*
* @author Fredrik Osterlind
*/
public class ProjectConfig {
@ -91,7 +91,7 @@ public class ProjectConfig {
/**
* Creates new project configuration.
*
*
* @param useDefault
* If true the default configuration will be loaded
* @throws FileNotFoundException
@ -105,6 +105,7 @@ public class ProjectConfig {
myConfig = new Properties();
myProjectDirHistory = new Vector<File>();
if (useDefault) {
InputStream input = GUI.class
.getResourceAsStream(GUI.PROJECT_DEFAULT_CONFIG_FILENAME);
@ -123,7 +124,7 @@ public class ProjectConfig {
/**
* Appends the given project directory's config file. This method also saves a
* local history of which project directories has been loaded.
*
*
* @param projectDir
* Project directory
* @return True if loaded OK
@ -147,7 +148,7 @@ public class ProjectConfig {
* element is non-null, then the project directory that added this element will be
* returned instead. If no such project directory can be found null is returned
* instead.
*
*
* @param callingClass
* Class which value belong to
* @param key
@ -162,14 +163,15 @@ public class ProjectConfig {
if (getStringValue(callingClass, key, null) == null) {
return null;
}
// Check that element really exists, if any
if (arrayElement != null) {
String[] array = getStringArrayValue(callingClass, key);
boolean foundValue = false;
for (int c=0; c < array.length; c++) {
if (array[c].equals(arrayElement))
for (String element : array) {
if (element.equals(arrayElement)) {
foundValue = true;
}
}
if (!foundValue) {
return null;
@ -179,16 +181,17 @@ public class ProjectConfig {
// Search in all project directory in reversed order
try {
ProjectConfig remadeConfig = new ProjectConfig(false);
for (int i=myProjectDirHistory.size()-1; i >= 0; i--) {
remadeConfig.appendProjectDir(myProjectDirHistory.get(i));
if (arrayElement != null) {
// Look for array
String[] array = remadeConfig.getStringArrayValue(callingClass, key);
for (int c=0; c < array.length; c++) {
if (array[c].equals(arrayElement))
for (String element : array) {
if (element.equals(arrayElement)) {
return myProjectDirHistory.get(i);
}
}
} else {
// Look for key
@ -197,12 +200,12 @@ public class ProjectConfig {
}
}
}
} catch (Exception e) {
logger.fatal("Exception when searching in project directory history: " + e);
return null;
}
return null;
}
@ -210,10 +213,10 @@ public class ProjectConfig {
* Loads the given property file and appends it to the current configuration.
* If a property already exists it will be overwritten, unless the new value
* begins with a '+' in which case the old value will be extended.
*
*
* WARNING! The project directory history will not be saved if this method is
* called, instead the appendUserPlatform method should be used.
*
*
* @param propertyFile
* Property file to read
* @return True if file was read ok, false otherwise
@ -233,10 +236,10 @@ public class ProjectConfig {
* configuration. If a property already exists it will be overwritten, unless
* the new value begins with a '+' in which case the old value will be
* extended.
*
*
* WARNING! The project directory history will not be saved if this method is
* called, instead the appendUserPlatform method should be used.
*
*
* @param configFileStream
* Stream to read from
* @return True if stream was read ok, false otherwise
@ -262,13 +265,15 @@ public class ProjectConfig {
String key = (String) en.nextElement();
String property = newProps.getProperty(key);
if (property.startsWith("+ ")) {
if (currentValues.getProperty(key) != null)
if (currentValues.getProperty(key) != null) {
currentValues.setProperty(key, currentValues.getProperty(key) + " "
+ property.substring(1).trim());
else
} else {
currentValues.setProperty(key, property.substring(1).trim());
} else
}
} else {
currentValues.setProperty(key, property);
}
}
return true;
@ -283,7 +288,7 @@ public class ProjectConfig {
/**
* Get string value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -312,21 +317,22 @@ public class ProjectConfig {
/**
* Returns value of given name.
*
*
* @param name
* Name
* @return Value as string
*/
public String getStringValue(String name) {
if (!myConfig.containsKey(name))
if (!myConfig.containsKey(name)) {
logger.debug("Could not find key named '" + name + "'");
}
return myConfig.getProperty(name);
}
/**
* Get string value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -339,7 +345,7 @@ public class ProjectConfig {
/**
* Get string array value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -348,15 +354,16 @@ public class ProjectConfig {
*/
public String[] getStringArrayValue(Class callingClass, String id) {
String stringVal = getStringValue(callingClass, id, null);
if (stringVal == null)
if (stringVal == null) {
return new String[0];
}
return getStringValue(callingClass, id, "").split(" ");
}
/**
* Get string value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -369,22 +376,23 @@ public class ProjectConfig {
/**
* Get string array value with given id.
*
*
* @param id
* Id of value to return
* @return Value or null if id wasn't found
*/
public String[] getStringArrayValue(String id) {
String stringVal = getStringValue(id);
if (stringVal == null)
if (stringVal == null) {
return new String[0];
}
return getStringValue(id).split(" ");
}
/**
* Get integer value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -395,15 +403,16 @@ public class ProjectConfig {
*/
public int getIntegerValue(Class callingClass, String id, int defaultValue) {
String str = getStringValue(callingClass, id);
if (str == null)
if (str == null) {
return defaultValue;
}
return Integer.parseInt(str);
}
/**
* Get integer value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -416,7 +425,7 @@ public class ProjectConfig {
/**
* Get double value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -428,15 +437,16 @@ public class ProjectConfig {
public double getDoubleValue(Class callingClass, String id,
double defaultValue) {
String str = getStringValue(callingClass, id);
if (str == null)
if (str == null) {
return defaultValue;
}
return Double.parseDouble(str);
}
/**
* Get double value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -449,7 +459,7 @@ public class ProjectConfig {
/**
* Get boolean value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id
@ -461,15 +471,16 @@ public class ProjectConfig {
public boolean getBooleanValue(Class callingClass, String id,
boolean defaultValue) {
String str = getStringValue(callingClass, id);
if (str == null)
if (str == null) {
return defaultValue;
}
return Boolean.parseBoolean(str);
}
/**
* Get boolean value with given id.
*
*
* @param callingClass
* Class which value belongs to
* @param id

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ExternalToolsDialog.java,v 1.9 2008/02/12 15:25:41 fros4943 Exp $
* $Id: ExternalToolsDialog.java,v 1.10 2008/02/18 08:18:18 fros4943 Exp $
*/
package se.sics.cooja.dialogs;
@ -65,6 +65,9 @@ public class ExternalToolsDialog extends JDialog {
* Parent container for dialog
*/
public static void showDialog(Container parentContainer) {
if (GUI.isVisualizedInApplet()) {
return;
}
ExternalToolsDialog myDialog = null;
if (parentContainer instanceof Window) {

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ProjectDirectoriesDialog.java,v 1.5 2008/02/12 15:06:09 fros4943 Exp $
* $Id: ProjectDirectoriesDialog.java,v 1.6 2008/02/18 08:18:18 fros4943 Exp $
*/
package se.sics.cooja.dialogs;
@ -81,6 +81,9 @@ public class ProjectDirectoriesDialog extends JDialog {
*/
public static Vector<File> showDialog(Container parentContainer,
Vector<File> changableProjects, Vector<File> fixedProjects) {
if (GUI.isVisualizedInApplet()) {
return null;
}
ProjectDirectoriesDialog myDialog = null;
if (parentContainer instanceof Window) {