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;
@ -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);
@ -167,9 +168,10 @@ public class ProjectConfig {
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;
@ -186,9 +188,10 @@ public class ProjectConfig {
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
@ -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;
@ -318,8 +323,9 @@ public class ProjectConfig {
* @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);
}
@ -348,8 +354,9 @@ 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(" ");
}
@ -376,8 +383,9 @@ public class ProjectConfig {
*/
public String[] getStringArrayValue(String id) {
String stringVal = getStringValue(id);
if (stringVal == null)
if (stringVal == null) {
return new String[0];
}
return getStringValue(id).split(" ");
}
@ -395,8 +403,9 @@ 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);
}
@ -428,8 +437,9 @@ 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);
}
@ -461,8 +471,9 @@ 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);
}

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) {