Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ bin/
*.ipr
*.iws
*.db
alitheia/cache/tmp/
alitheia/core/tmp/
alitheia/runner/
6 changes: 6 additions & 0 deletions alitheia/cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,11 @@
<version>4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@

public class Activator implements BundleActivator {

public static final String CACHE_IMPL_DEFAULT = "eu.sqooss.service.cache.OnDiskCache";
public static final String CACHE_IMPL_KEY = "eu.sqooss.service.cache.impl";

/**
* Registers CacheService implementation. If a system property with key
* CACHE_IMPL_KEY exists, the value is used as class. Otherwise,
* CACHE_IMPL_DFAULT is used.
*
* @param bc
* @throws Exception
*/
public void start(BundleContext bc) throws Exception {
AlitheiaCore.getInstance().registerService(CacheService.class, CacheServiceImpl.class);
String impl = bc.getProperty(CACHE_IMPL_KEY);

if (impl == null) {
impl = CACHE_IMPL_DEFAULT;
}

Class clazz = Thread.currentThread().getContextClassLoader().loadClass(impl);

AlitheiaCore.getInstance().registerService(CacheService.class, clazz);
}

public void stop(BundleContext bc) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package eu.sqooss.service.cache;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import eu.sqooss.service.logging.Logger;
import org.osgi.framework.BundleContext;

public abstract class BaseCacheServiceImpl implements CacheService {

private BundleContext bundleContext;
private Logger logger;

@Override
public void setInitParams(BundleContext bundleContext, Logger logger) {
this.bundleContext = bundleContext;
this.logger = logger;
}

@Override
public InputStream getStream(String key) {
byte[] buff = get(key);

if (buff == null) {
return null;
}

ByteArrayInputStream bais = new ByteArrayInputStream(buff);
return bais;
}

@Override
public void setStream(String key, InputStream in) {
try {
int nRead;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[4096];

while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}

buffer.flush();
set(key, buffer.toByteArray());

} catch (IOException e) {
logger.error("Error");
}
}

protected Logger getLogger() {
return logger;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

import java.util.concurrent.ConcurrentHashMap;

public class InMemoryCache extends CacheServiceImpl {
public class InMemoryCache extends BaseCacheServiceImpl {

ConcurrentHashMap<String, byte[]> cache = new ConcurrentHashMap<String, byte[]>(1024);

public InMemoryCache() {}


@Override
public boolean startUp() {
return true;
}

@Override
public void shutDown() {

}

@Override
public byte[] get(String key) {
return cache.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,44 @@
* </dl>
*
*/
public class OnDiskCache extends CacheServiceImpl {
public class OnDiskCache extends BaseCacheServiceImpl {

public static final String CACHE_DIR = "eu.sqooss.service.cache.dir";

private File dir;

private Logger log;

public OnDiskCache(String cachedir) throws Exception {
initDir(cachedir);
}

public OnDiskCache() throws Exception {

String dirpath = System.getProperty(CACHE_DIR);

if (dirpath == null) {
dirpath = System.getProperty("java.io.tmpdir");
if (dirpath == null)
if (dirpath == null) {
dirpath = "tmp";
}
}
initDir(dirpath);

}

@Override
public boolean startUp() {
return true;
}

@Override
public void shutDown() {

}

private void initDir(String path) throws Exception {
dir = new File(path);

if (!dir.exists())
if (!dir.exists()) {
dir.mkdirs();
}
}

@Override
Expand Down Expand Up @@ -170,15 +178,20 @@ public void set(String key, byte[] data) {
private String md5(String...args) throws NoSuchAlgorithmException {
MessageDigest m = MessageDigest.getInstance("MD5");

for (String arg : args)
for (String arg : args) {
m.update(arg.getBytes(), 0, arg.length());
}

return new BigInteger(1, m.digest()).toString(16);
}

private void warn(String message) {
if (log != null)
log.warn(message);
else
Logger logger = getLogger();

if (logger != null) {
logger.warn(message);
} else {
System.err.println(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package eu.sqooss.service.cache.test;

import eu.sqooss.core.AlitheiaCore;
import eu.sqooss.service.cache.Activator;
import eu.sqooss.service.cache.CacheService;
import eu.sqooss.service.cache.InMemoryCache;
import eu.sqooss.service.cache.OnDiskCache;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.BundleContext;

import static org.mockito.Mockito.*;

public class ActivatorTest {

AlitheiaCore alitheiaCore = mock(AlitheiaCore.class);
BundleContext bc = mock(BundleContext.class);
@Before
public void setUp() {
AlitheiaCore.setInstance(alitheiaCore);
}

@Test
public void testStart() throws Exception {
Activator activator = new Activator();
activator.start(bc);

verify(alitheiaCore, times(1)).registerService(CacheService.class, OnDiskCache.class);
}

@Test
public void testStartWithProperty() throws Exception {
when(bc.getProperty("eu.sqooss.service.cache.impl")).thenReturn("eu.sqooss.service.cache.InMemoryCache");

Activator activator = new Activator();
activator.start(bc);

verify(alitheiaCore, times(1)).registerService(CacheService.class, InMemoryCache.class);
}

}
Loading