Search in sources :

Example 11 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class Launcher method main.

public static void main(final String[] original_args) throws Exception {
    LogManager.getLogManager().readConfiguration(Launcher.class.getResourceAsStream("/logging.properties"));
    final Logger logger = Logger.getLogger(Launcher.class.getName());
    // Can't change default charset, but warn if it's not UTF-8.
    // Config files for displays, data browser etc. explicitly use XMLUtil.ENCODING = "UTF-8".
    // EPICS database files, strings in Channel Access or PVAccess are expected to use UTF-8.
    // New Java API like java.nio.file.Files defaults to UTF-8,
    // but library code including JCA simply calls new String(byte[]).
    // The underlying Charset.defaultCharset() checks "file.encoding",
    // but this happens at an early stage of VM startup.
    // Calling System.setPropertu("file.encoding", "UTF-8") in main() is already too late,
    // must add -D"file.encoding=UTF-8" to java start up or JAVA_TOOL_OPTIONS.
    final Charset cs = Charset.defaultCharset();
    if (!"UTF-8".equalsIgnoreCase(cs.displayName())) {
        logger.severe("Default charset is " + cs.displayName() + " instead of UTF-8.");
        logger.severe("Add    -D\"file.encoding=UTF-8\"    to java command line or JAVA_TOOL_OPTIONS");
    }
    Locations.initialize();
    // Check for site-specific settings.ini bundled into distribution
    // before potentially adding command-line settings.
    final File site_settings = new File(Locations.install(), "settings.ini");
    if (site_settings.canRead()) {
        logger.log(Level.CONFIG, "Loading settings from " + site_settings);
        PropertyPreferenceLoader.load(new FileInputStream(site_settings));
    }
    // Handle arguments, potentially not even starting the UI
    final List<String> args = new ArrayList<>(List.of(original_args));
    final Iterator<String> iter = args.iterator();
    int port = -1;
    try {
        while (iter.hasNext()) {
            final String cmd = iter.next();
            if (cmd.startsWith("-h")) {
                help();
                return;
            }
            if (cmd.equals("-splash")) {
                iter.remove();
                Preferences.userNodeForPackage(org.phoebus.ui.Preferences.class).putBoolean(org.phoebus.ui.Preferences.SPLASH, true);
            } else if (cmd.equals("-nosplash")) {
                iter.remove();
                Preferences.userNodeForPackage(org.phoebus.ui.Preferences.class).putBoolean(org.phoebus.ui.Preferences.SPLASH, false);
            } else if (cmd.equals("-logging")) {
                if (!iter.hasNext())
                    throw new Exception("Missing -logging file name");
                iter.remove();
                final String filename = iter.next();
                iter.remove();
                LogManager.getLogManager().readConfiguration(new FileInputStream(filename));
            } else if (cmd.equals("-settings")) {
                if (!iter.hasNext())
                    throw new Exception("Missing -settings file name");
                iter.remove();
                final String filename = iter.next();
                iter.remove();
                logger.info("Loading settings from " + filename);
                if (filename.endsWith(".xml"))
                    Preferences.importPreferences(new FileInputStream(filename));
                else
                    PropertyPreferenceLoader.load(new FileInputStream(filename));
            } else if (cmd.equals("-export_settings")) {
                if (!iter.hasNext())
                    throw new Exception("Missing -export_settings file name");
                iter.remove();
                final String filename = iter.next();
                iter.remove();
                System.out.println("Exporting settings to " + filename);
                Preferences.userRoot().node("org/phoebus").exportSubtree(new FileOutputStream(filename));
                return;
            } else if (cmd.equals("-server")) {
                if (!iter.hasNext())
                    throw new Exception("Missing -server port");
                iter.remove();
                port = Integer.parseInt(iter.next());
                iter.remove();
            } else if (cmd.equals("-list")) {
                iter.remove();
                final Collection<AppDescriptor> apps = ApplicationService.getApplications();
                System.out.format("Name                 Description          File Extensions\n");
                for (AppDescriptor app : apps) {
                    if (app instanceof AppResourceDescriptor) {
                        final AppResourceDescriptor app_res = (AppResourceDescriptor) app;
                        System.out.format("%-20s %-20s %s\n", "'" + app.getName() + "'", app.getDisplayName(), app_res.supportedFileExtentions().stream().collect(Collectors.joining(", ")));
                    } else
                        System.out.format("%-20s %s\n", "'" + app.getName() + "'", app.getDisplayName());
                }
                return;
            } else if (cmd.equals("-main")) {
                iter.remove();
                if (!iter.hasNext())
                    throw new Exception("Missing -main name");
                final String main = iter.next();
                iter.remove();
                // Locate Main class and its main()
                final Class<?> main_class = Class.forName(main);
                final Method main_method = main_class.getDeclaredMethod("main", String[].class);
                // Collect remaining arguments
                final List<String> new_args = new ArrayList<>();
                iter.forEachRemaining(new_args::add);
                main_method.invoke(null, new Object[] { new_args.toArray(new String[new_args.size()]) });
                return;
            }
        }
    } catch (Exception ex) {
        help();
        System.out.println();
        ex.printStackTrace();
        return;
    }
    logger.info("Phoebus (PID " + ProcessHandle.current().pid() + ")");
    // instead of starting a new application
    if (port > 0) {
        final ApplicationServer server = ApplicationServer.create(port);
        if (!server.isServer()) {
            server.sendArguments(args);
            return;
        }
    }
    // Remaining args passed on
    Application.launch(PhoebusApplication.class, args.toArray(new String[args.size()]));
}
Also used : ArrayList(java.util.ArrayList) Logger(java.util.logging.Logger) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) AppDescriptor(org.phoebus.framework.spi.AppDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Preferences(java.util.prefs.Preferences) Charset(java.nio.charset.Charset) Method(java.lang.reflect.Method) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) ApplicationServer(org.phoebus.ui.application.ApplicationServer) File(java.io.File)

Example 12 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class ApplicationLauncherService method openResource.

/**
 * @param resource Resource received as command line argument
 * @param prompt Prompt if there are multiple applications, or use first one?
 * @param stage If prompt is enabled, a selection dialog will be launched
 *              positioned next to the provided stage. If <code>null</code> then the
 *              default or first application will be used
 * @return <code>true</code> if resource could be opened
 */
public static boolean openResource(final URI resource, final boolean prompt, final Stage stage) {
    final AppResourceDescriptor application = findApplication(resource, prompt, stage);
    if (application == null)
        return false;
    logger.log(Level.INFO, "Opening " + resource + " with " + application.getName());
    application.create(resource);
    return true;
}
Also used : AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor)

Aggregations

AppResourceDescriptor (org.phoebus.framework.spi.AppResourceDescriptor)12 URI (java.net.URI)7 ImageView (javafx.scene.image.ImageView)7 File (java.io.File)6 Logger (java.util.logging.Logger)5 MenuItem (javafx.scene.control.MenuItem)5 List (java.util.List)4 Level (java.util.logging.Level)4 SeparatorMenuItem (javafx.scene.control.SeparatorMenuItem)4 Image (javafx.scene.image.Image)4 FileInputStream (java.io.FileInputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 FXML (javafx.fxml.FXML)3 Stage (javafx.stage.Stage)3 ApplicationService (org.phoebus.framework.workbench.ApplicationService)3 IOException (java.io.IOException)2 Optional (java.util.Optional)2 Dialog (javafx.scene.control.Dialog)2