Search in sources :

Example 1 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class Util method printMessages.

/**
 * Print set of messages into output
 *
 * @param out output
 * @param set set of messages
 * @param limited if the container should be limited
 */
public static void printMessages(Writer out, SortedSet<Message> set, boolean limited) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    if (!set.isEmpty()) {
        try {
            out.write("<ul class=\"message-group");
            if (limited) {
                out.write(" limited");
            }
            out.write("\">\n");
            for (Message m : set) {
                out.write("<li class=\"message-group-item ");
                out.write(Util.encode(m.getClassName()));
                out.write("\" title=\"Expires on ");
                out.write(Util.encode(df.format(m.getExpiration())));
                out.write("\">");
                out.write(Util.encode(df.format(m.getCreated())));
                out.write(": ");
                out.write(m.getText());
                out.write("</li>");
            }
            out.write("</ul>");
        } catch (IOException ex) {
            LOGGER.log(Level.WARNING, "An error occurred for a group of messages", ex);
        }
    }
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class Indexer method main.

/**
 * Program entry point
 *
 * @param argv argument vector
 */
@SuppressWarnings("PMD.UseStringBufferForStringAppends")
public static void main(String[] argv) {
    // this won't count JVM creation though
    Statistics stats = new Statistics();
    boolean update = true;
    Executor.registerErrorHandler();
    ArrayList<String> subFiles = new ArrayList<>();
    ArrayList<String> subFilesList = new ArrayList<>();
    boolean listFiles = false;
    boolean createDict = false;
    try {
        argv = parseOptions(argv);
        if (help) {
            status = 1;
            System.err.println(helpUsage);
            if (helpDetailed) {
                System.err.println(AnalyzerGuruHelp.getUsage());
                System.err.println(ConfigurationHelp.getSamples());
            }
            System.exit(status);
        }
        if (awaitProfiler)
            pauseToAwaitProfiler();
        env = RuntimeEnvironment.getInstance();
        // Complete the configuration of repository types.
        List<Class<? extends Repository>> repositoryClasses = RepositoryFactory.getRepositoryClasses();
        for (Class<? extends Repository> clazz : repositoryClasses) {
            // Set external repository binaries from System properties.
            try {
                Field f = clazz.getDeclaredField("CMD_PROPERTY_KEY");
                Object key = f.get(null);
                if (key != null) {
                    cfg.setRepoCmd(clazz.getCanonicalName(), System.getProperty(key.toString()));
                }
            } catch (Exception e) {
            // don't care
            }
        }
        // Logging starts here.
        if (cfg.isVerbose()) {
            String fn = LoggerUtil.getFileHandlerPattern();
            if (fn != null) {
                System.out.println("Logging filehandler pattern: " + fn);
            }
        }
        // automatically allow symlinks that are directly in source root
        String file = cfg.getSourceRoot();
        if (file != null) {
            File sourceRootFile = new File(file);
            File[] projectDirs = sourceRootFile.listFiles();
            if (projectDirs != null) {
                for (File projectDir : projectDirs) {
                    if (!projectDir.getCanonicalPath().equals(projectDir.getAbsolutePath())) {
                        allowedSymlinks.add(projectDir.getAbsolutePath());
                    }
                }
            }
        }
        allowedSymlinks.addAll(cfg.getAllowedSymlinks());
        cfg.setAllowedSymlinks(allowedSymlinks);
        // grained checking in invalidateRepositories().
        for (int optind = 0; optind < argv.length; optind++) {
            String path = Paths.get(cfg.getSourceRoot(), argv[optind]).toString();
            subFilesList.add(path);
        }
        // according to the project key which is the same.
        for (Entry<String, Project> entry : cfg.getProjects().entrySet()) {
            if (entry.getValue().getName() == null) {
                entry.getValue().setName(entry.getKey());
            }
        }
        // Set updated configuration in RuntimeEnvironment.
        env.setConfiguration(cfg, subFilesList);
        // Let repository types to add items to ignoredNames.
        // This changes env so is called after the setConfiguration()
        // call above.
        RepositoryFactory.initializeIgnoredNames(env);
        if (noindex) {
            getInstance().sendToConfigHost(env, host, port);
            writeConfigToFile(env, configFilename);
            System.exit(0);
        }
        /*
             * Add paths to directories under source root. If projects
             * are enabled the path should correspond to a project because
             * project path is necessary to correctly set index directory
             * (otherwise the index files will end up in index data root
             * directory and not per project data root directory).
             * For the check we need to have 'env' already set.
             */
        for (String path : subFilesList) {
            String srcPath = env.getSourceRootPath();
            if (srcPath == null) {
                System.err.println("Error getting source root from environment. Exiting.");
                System.exit(1);
            }
            path = path.substring(srcPath.length());
            if (env.hasProjects()) {
                // The paths need to correspond to a project.
                if (Project.getProject(path) != null) {
                    subFiles.add(path);
                } else {
                    System.err.println("The path " + path + " does not correspond to a project");
                }
            } else {
                subFiles.add(path);
            }
        }
        if (!subFilesList.isEmpty() && subFiles.isEmpty()) {
            System.err.println("None of the paths were added, exiting");
            System.exit(1);
        }
        // emitted during indexing do not cause validation error.
        if (addProjects && host != null && port > 0) {
            Message m = Message.createMessage("config");
            m.addTag("set");
            m.setText("projectsEnabled = true");
            try {
                m.write(host, port);
            } catch (ConnectException ce) {
                LOGGER.log(Level.SEVERE, "Misconfig of webapp host or port", ce);
                System.err.println("Couldn't notify the webapp (and host or port set): " + ce.getLocalizedMessage());
            }
        }
        // Get history first.
        getInstance().prepareIndexer(env, searchRepositories, addProjects, defaultProjects, listFiles, createDict, subFiles, repositories, zapCache, listRepos);
        if (listRepos || !zapCache.isEmpty()) {
            return;
        }
        // And now index it all.
        if (runIndex || (optimizedChanged && env.isOptimizeDatabase())) {
            IndexChangedListener progress = new DefaultIndexChangedListener();
            getInstance().doIndexerExecution(update, subFiles, progress);
        }
        writeConfigToFile(env, configFilename);
        // or send new configuration to the web application in the case of full reindex.
        if (host != null) {
            if (!subFiles.isEmpty()) {
                getInstance().refreshSearcherManagers(env, subFiles, host, port);
            } else {
                getInstance().sendToConfigHost(env, host, port);
            }
        }
    } catch (ParseException e) {
        System.err.println("** " + e.getMessage());
        System.exit(1);
    } catch (IndexerException ex) {
        LOGGER.log(Level.SEVERE, "Exception running indexer", ex);
        System.err.println(openGrok.getUsage());
        System.exit(1);
    } catch (Throwable e) {
        LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
        System.err.println("Exception: " + e.getLocalizedMessage());
        System.exit(1);
    } finally {
        stats.report(LOGGER);
    }
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) ArrayList(java.util.ArrayList) Statistics(org.opensolaris.opengrok.util.Statistics) HistoryException(org.opensolaris.opengrok.history.HistoryException) ConnectException(java.net.ConnectException) ParseException(java.text.ParseException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) Project(org.opensolaris.opengrok.configuration.Project) Repository(org.opensolaris.opengrok.history.Repository) ParseException(java.text.ParseException) File(java.io.File) ConnectException(java.net.ConnectException)

Example 3 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method startConfigurationListenerThread.

/**
 * Start a thread to listen on a socket to receive new messages.
 * The messages can contain various commands for the webapp, including
 * upload of new configuration.
 *
 * @param endpoint The socket address to listen on
 * @return true if the endpoint was available (and the thread was started)
 */
public boolean startConfigurationListenerThread(SocketAddress endpoint) {
    boolean ret = false;
    try {
        configServerSocket = new ServerSocket();
        configServerSocket.bind(endpoint);
        ret = true;
        final ServerSocket sock = configServerSocket;
        configurationListenerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1 << 15);
                while (!sock.isClosed()) {
                    try (Socket s = sock.accept();
                        BufferedInputStream in = new BufferedInputStream(new XmlEofInputStream(s.getInputStream()));
                        OutputStream output = s.getOutputStream()) {
                        bos.reset();
                        LOGGER.log(Level.FINE, "OpenGrok: Got request from {0}", s.getInetAddress().getHostAddress());
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) != -1) {
                            bos.write(buf, 0, len);
                        }
                        buf = bos.toByteArray();
                        if (LOGGER.isLoggable(Level.FINE)) {
                            LOGGER.log(Level.FINE, "new config:{0}", new String(buf));
                        }
                        Object obj;
                        try (XMLDecoder d = new XMLDecoder(new ByteArrayInputStream(buf))) {
                            obj = d.readObject();
                        }
                        if (obj instanceof Message) {
                            Message m = ((Message) obj);
                            handleMessage(m, output);
                        }
                    } catch (IOException e) {
                        LOGGER.log(Level.SEVERE, "Error reading config file: ", e);
                    } catch (RuntimeException e) {
                        LOGGER.log(Level.SEVERE, "Error parsing config file: ", e);
                    }
                }
            }
        }, "configurationListener");
        configurationListenerThread.start();
    } catch (UnknownHostException ex) {
        LOGGER.log(Level.WARNING, "Problem resolving sender: ", ex);
    } catch (IOException ex) {
        LOGGER.log(Level.WARNING, "I/O error when waiting for config: ", ex);
    }
    if (!ret && configServerSocket != null) {
        IOUtils.close(configServerSocket);
    }
    return ret;
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) UnknownHostException(java.net.UnknownHostException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) XmlEofInputStream(org.opensolaris.opengrok.util.XmlEofInputStream) JSONObject(org.json.simple.JSONObject) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 4 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class Util method messagesToJson.

/**
 * Print set of messages into json array
 *
 * @param set set of messages
 * @return json array containing the set of messages
 */
@SuppressWarnings("unchecked")
public static JSONArray messagesToJson(SortedSet<Message> set) {
    JSONArray array = new JSONArray();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    for (Message m : set) {
        JSONObject message = new JSONObject();
        message.put("class", Util.encode(m.getClassName()));
        message.put("expiration", Util.encode(df.format(m.getExpiration())));
        message.put("created", Util.encode(df.format(m.getCreated())));
        message.put("text", Util.encode(m.getText()));
        JSONArray tags = new JSONArray();
        for (String t : m.getTags()) {
            tags.add(Util.encode(t));
        }
        message.put("tags", tags);
        array.add(message);
    }
    return array;
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class Util method messagesToJsonObject.

/**
 * Print set of messages into json object for given tag.
 *
 * @param tag return messages in json format for the given tag
 * @return json object with 'tag' and 'messages' attribute or null
 */
@SuppressWarnings("unchecked")
public static JSONObject messagesToJsonObject(String tag) {
    SortedSet<Message> messages = RuntimeEnvironment.getInstance().getMessages(tag);
    if (messages.isEmpty()) {
        return null;
    }
    JSONObject toRet = new JSONObject();
    toRet.put("tag", tag);
    toRet.put("messages", messagesToJson(messages));
    return toRet;
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) JSONObject(org.json.simple.JSONObject)

Aggregations

Message (org.opensolaris.opengrok.configuration.messages.Message)9 IOException (java.io.IOException)4 JSONObject (org.json.simple.JSONObject)3 File (java.io.File)2 ConnectException (java.net.ConnectException)2 UnknownHostException (java.net.UnknownHostException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 XMLDecoder (java.beans.XMLDecoder)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)1 ParseException (java.text.ParseException)1