Skip to content

Commit

Permalink
add a little helper method to load all the JSON files matching a file…
Browse files Browse the repository at this point in the history
… wildcard and containing the given text as a single blob of JSON for hawtSpot table viewing
  • Loading branch information
jstrachan committed Mar 26, 2013
1 parent b3fe230 commit 1f011b6
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 21 deletions.
81 changes: 60 additions & 21 deletions hawtio-git/src/main/java/io/hawt/git/GitFacade.java
@@ -1,6 +1,8 @@
package io.hawt.git;

import io.hawt.io.FileFilters;
import io.hawt.io.IOHelper;
import io.hawt.io.Strings;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.CommitCommand;
Expand Down Expand Up @@ -31,6 +33,7 @@
import javax.management.ObjectName;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
Expand Down Expand Up @@ -160,7 +163,7 @@ public FileContents read(String branch, String path) throws IOException {
if (file.exists()) {
File[] files = file.listFiles();
for (File child : files) {
if (!child.getName().equals(".git")) {
if (!isIgnoreFile(child)) {
children.add(FileInfo.createFileInfo(rootDir, child));
}
}
Expand All @@ -169,6 +172,56 @@ public FileContents read(String branch, String path) throws IOException {
}
}

protected boolean isIgnoreFile(File child) {
return child.getName().equals(".git");
}

/**
* Reads the child JSON file contents which match the given search string (if specified) and which match the given file name wildcard (using * to match any characters in the name).
* @return
*/
@Override
public String readJsonChildContent(String branch, String path, String fileNameWildcard, String search) throws IOException {
if (!Strings.isNotBlank(fileNameWildcard)) {
fileNameWildcard = "*.json";
}
return readChildContents(path, fileNameWildcard, search, "[\n", ",\n", "\n]");
}

/**
* Returns the child file contents which match the given name wildcard (using * to match any sequence of characters) and search string (if specified.
*/
@Override
public String readChildContents(String path, String fileNameWildcard, String search, String prefix, String separator, String postfix) throws IOException {
File rootDir = getConfigDirectory();
File file = getFile(path);
FileFilter filter = FileFilters.createFileFilter(fileNameWildcard);
boolean first = true;
StringBuilder buffer = new StringBuilder(prefix);
List<FileInfo> children = new ArrayList<FileInfo>();
if (file.isDirectory()) {
if (file.exists()) {
File[] files = file.listFiles();
for (File child : files) {
if (!isIgnoreFile(child) && child.isFile()) {
String text = IOHelper.readFully(child);
if (!Strings.isNotBlank(search) || text.contains(search)) {
if (first) {
first = false;
} else {
buffer.append(separator);
}
buffer.append(text);
children.add(FileInfo.createFileInfo(rootDir, child));
}
}
}
}
}
buffer.append(postfix);
return buffer.toString();
}


public void write(final String branch, final String path, final String commitMessage,
final String authorName, final String authorEmail, final String contents) {
Expand Down Expand Up @@ -243,15 +296,15 @@ public List<CommitInfo> history(String objectId, String path, int limit) {

CommitFinder finder = new CommitFinder(r);
CommitListFilter block = new CommitListFilter();
if (isNotBlank(path)) {
if (Strings.isNotBlank(path)) {
finder.setFilter(PathFilterUtils.and(path));
}
finder.setFilter(block);

if (limit > 0) {
finder.setFilter(new CommitLimitFilter(100).setStop(true));
}
if (isNotBlank(objectId)) {
if (Strings.isNotBlank(objectId)) {
finder.findFrom(objectId);
} else {
finder.find();
Expand All @@ -263,7 +316,7 @@ public List<CommitInfo> history(String objectId, String path, int limit) {
String author = entry.getAuthorIdent().getName();
boolean merge = entry.getParentCount() > 1;
String shortMessage = entry.getShortMessage();
String trimmedMessage = trimString(shortMessage, 78);
String trimmedMessage = Strings.trimString(shortMessage, 78);
String name = entry.getName();
String commitHashText = getShortCommitHash(name);
results.add(new CommitInfo(commitHashText, name, author, date, merge, trimmedMessage, shortMessage));
Expand All @@ -287,20 +340,6 @@ public static Date getCommitDate(RevCommit commit) {
return new Date(commit.getCommitTime() * 1000L);
}

public static String trimString(String value, int max) {
if (value == null) {
return "";
}
if (value.length() <= max) {
return value;
}
return value.substring(0, max - 3) + "...";
}

public static boolean isNotBlank(String text) {
return text != null && text.trim().length() > 0;
}

@Override
public String diff(String objectId, String baseObjectId, String path) {
Repository r = git.getRepository();
Expand Down Expand Up @@ -332,13 +371,13 @@ public String diff(String objectId, String baseObjectId, String path) {
*/

RevCommit commit;
if (isNotBlank(objectId)) {
if (Strings.isNotBlank(objectId)) {
commit = CommitUtils.getCommit(r, objectId);
} else {
commit = CommitUtils.getHead(r);
}
RevCommit baseCommit = null;
if (isNotBlank(baseObjectId)) {
if (Strings.isNotBlank(baseObjectId)) {
baseCommit = CommitUtils.getCommit(r, baseObjectId);
}

Expand Down Expand Up @@ -442,7 +481,7 @@ public void initialiseGitRepo() throws IOException, GitAPIException {
File gitDir = new File(confDir, ".git");
if (!gitDir.exists()) {
String repo = getRemoteRepository();
if (isNotBlank(repo) && isCloneRemoteRepoOnStartup()) {
if (Strings.isNotBlank(repo) && isCloneRemoteRepoOnStartup()) {
LOG.info("Cloning git repo " + repo + " into directory " + confDir.getCanonicalPath());
CloneCommand clone = Git.cloneRepository().setURI(repo).setDirectory(confDir).setRemote(remote);
try {
Expand Down
4 changes: 4 additions & 0 deletions hawtio-git/src/main/java/io/hawt/git/GitFacadeMXBean.java
Expand Up @@ -45,4 +45,8 @@ void remove(String branch, String path, String commitMessage,
*/
void revertTo(String branch, String objectId, String blobPath, String commitMessage,
String authorName, String authorEmail);

String readJsonChildContent(String branch, String path, String fileNameWildcard, String search) throws IOException;

String readChildContents(String path, String fileNameWildcard, String search, String prefix, String separator, String postfix) throws IOException;
}
118 changes: 118 additions & 0 deletions hawtio-git/src/main/java/io/hawt/io/FileFilters.java
@@ -0,0 +1,118 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.hawt.io;

import java.io.File;
import java.io.FileFilter;

/**
* A number of helper functions for creating {@link FileFilter} objects on a {@link File} object.
*/
public class FileFilters {
private static final FileFilter TRUE_FILTER = new FileFilter() {
@Override
public String toString() {
return "TrueFileFilter";
}

@Override
public boolean accept(File pathname) {
return true;
}
};

public static FileFilter createFileFilter(String wildcard) {
if (Strings.isNotBlank(wildcard)) {
int idx = wildcard.indexOf('*');
if (idx < 0) {
return nameEqualsFilter(wildcard);
}
int lastIdx = wildcard.lastIndexOf(idx);
if (lastIdx < 0) {
lastIdx = idx;
}
FileFilter endsWith = nameEndsWithFilter(wildcard.substring(lastIdx + 1));
if (idx <= 0) {
return endsWith;
} else {
return andFilter(nameStartsWithFilter(wildcard.substring(0, idx)), endsWith);
}
}
return trueFilter();
}

public static FileFilter nameEqualsFilter(final String name) {
return new FileFilter() {
@Override
public String toString() {
return "FileNameEqualsFilter(" + name + ")";
}

@Override
public boolean accept(File file) {
return name.equals(file.getName());
}
};
}

public static FileFilter nameStartsWithFilter(final String name) {
return new FileFilter() {
@Override
public String toString() {
return "FileNameStartsWithFilter(" + name + ")";
}

@Override
public boolean accept(File file) {
return file.getName().startsWith(name);
}
};
}

public static FileFilter nameEndsWithFilter(final String name) {
return new FileFilter() {
@Override
public String toString() {
return "FileNameEndsWithFilter(" + name + ")";
}

@Override
public boolean accept(File file) {
return file.getName().endsWith(name);
}
};
}

public static FileFilter andFilter(final FileFilter filter1, final FileFilter filter2) {
return new FileFilter() {
@Override
public String toString() {
return "AndFilter(" + filter1 + " && " + filter2 + ")";
}

@Override
public boolean accept(File file) {
return filter1.accept(file) && filter2.accept(file);
}
};
}

public static FileFilter trueFilter() {
return TRUE_FILTER;
}
}
36 changes: 36 additions & 0 deletions hawtio-git/src/main/java/io/hawt/io/Strings.java
@@ -0,0 +1,36 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.hawt.io;

/**
*/
public class Strings {
public static String trimString(String value, int max) {
if (value == null) {
return "";
}
if (value.length() <= max) {
return value;
}
return value.substring(0, max - 3) + "...";
}

public static boolean isNotBlank(String text) {
return text != null && text.trim().length() > 0;
}
}
18 changes: 18 additions & 0 deletions hawtio-git/src/test/java/io/hawt/git/GitFacadeTest.java
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -147,6 +148,23 @@ public void createFileAndListDirectory() throws Exception {
String content = git.getContent(info.getName(), path);
System.out.println(" = " + content);
}


// write some JSON files then lets combine them in a single read
String jsonDir = "/foo";
git.write(branch, jsonDir + "/" + "1.json", "Initial commit", authorName, authorEmail, "{ key: 1, name: 'James'}");
git.write(branch, jsonDir + "/" + "2.json", "Initial commit", authorName, authorEmail, "{ key: 2, name: 'Stan'}");

// now lets read the JSON for the directory
String json = git.readJsonChildContent(branch, jsonDir, "*.json", null);
System.out.println("Got JSON: " + json);
assertTrue("JSON should include James but was: " + json, json.contains("James"));

json = git.readJsonChildContent(branch, jsonDir, "*.json", "James");
assertTrue("JSON should include James but was: " + json, json.contains("James"));

json = git.readJsonChildContent(branch, jsonDir, "*.json", "Stan");
assertFalse("JSON should not include James but was: " + json, json.contains("James"));
}

protected String assertReadFileContents(String path, String expectedContents) throws IOException {
Expand Down

0 comments on commit 1f011b6

Please sign in to comment.