Search in sources :

Example 66 with Pattern

use of java.util.regex.Pattern in project zeppelin by apache.

the class NotebookServer method restoreFolder.

private void restoreFolder(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws SchedulerException, IOException {
    String folderId = (String) fromMessage.get("id");
    if (folderId == null) {
        return;
    }
    Folder folder = notebook.getFolder(folderId);
    if (folder != null && folder.isTrash()) {
        String restoreName = folder.getId().replaceFirst(Folder.TRASH_FOLDER_ID + "/", "").trim();
        // if the folder had conflict when it had moved to trash before
        Pattern p = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$");
        Matcher m = p.matcher(restoreName);
        restoreName = m.replaceAll("").trim();
        fromMessage.put("name", restoreName);
        renameFolder(conn, userAndRoles, notebook, fromMessage, "restore");
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Folder(org.apache.zeppelin.notebook.Folder)

Example 67 with Pattern

use of java.util.regex.Pattern in project zeppelin by apache.

the class MongoNotebookRepo method printDuplicatedException.

/**
   * MongoBulkWriteException contains error messages that inform
   * which documents were duplicated. This method catches those ID and print them.
   * @param e
   */
private void printDuplicatedException(MongoBulkWriteException e) {
    List<BulkWriteError> errors = e.getWriteErrors();
    for (BulkWriteError error : errors) {
        String msg = error.getMessage();
        // regex for note ID
        Pattern pattern = Pattern.compile("[A-Z0-9]{9}");
        Matcher matcher = pattern.matcher(msg);
        if (matcher.find()) {
            // if there were a note ID
            String noteId = matcher.group();
            LOG.warn("Note " + noteId + " not inserted since already exists in MongoDB");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BulkWriteError(com.mongodb.bulk.BulkWriteError)

Example 68 with Pattern

use of java.util.regex.Pattern in project zeppelin by apache.

the class InstallInterpreter method readAvailableInterpreters.

private void readAvailableInterpreters() throws IOException {
    if (!interpreterListFile.isFile()) {
        System.err.println("Can't find interpreter list " + interpreterListFile.getAbsolutePath());
        return;
    }
    String text = FileUtils.readFileToString(interpreterListFile);
    String[] lines = text.split("\n");
    Pattern pattern = Pattern.compile("(\\S+)\\s+(\\S+)\\s+(.*)");
    int lineNo = 0;
    for (String line : lines) {
        lineNo++;
        if (line == null || line.length() == 0 || line.startsWith("#")) {
            continue;
        }
        Matcher match = pattern.matcher(line);
        if (match.groupCount() != 3) {
            System.err.println("Error on line " + lineNo + ", " + line);
            continue;
        }
        match.find();
        String name = match.group(1);
        String artifact = match.group(2);
        String description = match.group(3);
        availableInterpreters.add(new AvailableInterpreterInfo(name, artifact, description));
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 69 with Pattern

use of java.util.regex.Pattern in project zookeeper by apache.

the class Log4JSource method init.

private void init() throws IOException {
    File f = new File(file);
    RandomAccessFileReader in = new RandomAccessFileReader(f);
    SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT);
    Pattern idp = Pattern.compile("\\[myid:(\\d+)\\]");
    long lastFp = in.getPosition();
    String line = in.readLine();
    Matcher m = null;
    // if we have read data from the file, and it matchs the timep pattern
    if ((line != null) && (m = timep.matcher(line)).lookingAt()) {
        starttime = timestampFromText(dateformat, m.group(1));
    } else {
        throw new IOException("Invalid log4j format. First line doesn't start with time");
    }
    /*
	  Count number of log entries. Any line starting with a timestamp counts as an entry
	*/
    String lastentry = line;
    try {
        while (line != null) {
            m = timep.matcher(line);
            if (m.lookingAt()) {
                if (size % skipN == 0) {
                    long time = timestampFromText(dateformat, m.group(1));
                    skiplist.addMark(time, lastFp, size);
                }
                size++;
                lastentry = line;
            }
            if (serverid == 0 && (m = idp.matcher(line)).find()) {
                serverid = Integer.valueOf(m.group(1));
            }
            lastFp = in.getPosition();
            line = in.readLine();
        }
    } catch (EOFException eof) {
    // ignore, simply end of file, though really (line!=null) should have caught this
    } finally {
        in.close();
    }
    m = timep.matcher(lastentry);
    if (m.lookingAt()) {
        endtime = timestampFromText(dateformat, m.group(1));
    } else {
        throw new IOException("Invalid log4j format. Last line doesn't start with time");
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) EOFException(java.io.EOFException) IOException(java.io.IOException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 70 with Pattern

use of java.util.regex.Pattern in project zookeeper by apache.

the class QuorumPeerMainTest method testBadPeerAddressInQuorum.

/**
     * Verify handling of bad quorum address
     */
@Test
public void testBadPeerAddressInQuorum() throws Exception {
    ClientBase.setupTestEnv();
    // setup the logger to capture all logs
    Layout layout = Logger.getRootLogger().getAppender("CONSOLE").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setThreshold(Level.WARN);
    Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
    qlogger.addAppender(appender);
    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();
        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\nserver.2=fee.fii.foo.fum:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2;
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        q1.start();
        boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000);
        Assert.assertFalse("Server never came up", isup);
        q1.shutdown();
        Assert.assertTrue("waiting for server 1 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT));
    } finally {
        qlogger.removeAppender(appender);
    }
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p = Pattern.compile(".*Cannot open channel to .* at election address .*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("complains about host", found);
}
Also used : Pattern(java.util.regex.Pattern) Layout(org.apache.log4j.Layout) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) LineNumberReader(java.io.LineNumberReader) Test(org.junit.Test)

Aggregations

Pattern (java.util.regex.Pattern)2745 Matcher (java.util.regex.Matcher)1806 ArrayList (java.util.ArrayList)334 Test (org.junit.Test)209 IOException (java.io.IOException)206 File (java.io.File)172 HashMap (java.util.HashMap)144 Field (java.lang.reflect.Field)118 BufferedReader (java.io.BufferedReader)108 Map (java.util.Map)97 PatternSyntaxException (java.util.regex.PatternSyntaxException)97 List (java.util.List)81 HashSet (java.util.HashSet)69 InputStreamReader (java.io.InputStreamReader)59 FileInputStream (java.io.FileInputStream)40 InputStream (java.io.InputStream)39 FileReader (java.io.FileReader)36 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 URL (java.net.URL)29 LinkedHashMap (java.util.LinkedHashMap)28