Skip to content
Snippets Groups Projects
Commit 0ef85208 authored by anttil's avatar anttil
Browse files

BIS-518 / SP-827: Extract the mechanism of enabling/disabling the logging by...

BIS-518 / SP-827: Extract the mechanism of enabling/disabling the logging by touching a control file to the common project. Initial commit.

SVN: 29677
parent 66f7e49e
No related branches found
No related tags found
No related merge requests found
Showing
with 533 additions and 0 deletions
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import java.util.HashMap;
import java.util.Map;
public class DelayingDecorator implements IEventProvider
{
private final IEventProvider provider;
private IClock clock;
private final long interval;
private long lastCall;
public DelayingDecorator(long interval, IEventProvider provider)
{
this(interval, provider, new SystemClock());
}
DelayingDecorator(long interval, IEventProvider provider, IClock clock)
{
this.provider = provider;
this.clock = clock;
this.interval = interval;
this.lastCall = 0;
}
@Override
public Map<String, String> getNewEvents()
{
long currentTime = clock.getTime();
if (currentTime - lastCall > interval)
{
lastCall = currentTime;
return provider.getNewEvents();
} else
{
return new HashMap<String, String>();
}
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class FileSystemBasedEventProvider implements IEventProvider
{
private final File controlDir;
public FileSystemBasedEventProvider(File controlDir)
{
this.controlDir = controlDir;
}
@Override
public Map<String, String> getNewEvents()
{
Map<String, String> map = new HashMap<String, String>();
for (File file : controlDir.listFiles())
{
String fileName = file.getName();
if (file.isFile() && fileName.contains("-"))
{
String key = fileName.substring(0, fileName.lastIndexOf("-"));
String value = fileName.substring(fileName.lastIndexOf("-") + 1);
map.put(key, value);
file.delete();
}
}
return map;
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
/**
* @author anttil
*/
interface IClock
{
public long getTime();
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import java.util.Map;
/**
* @author anttil
*/
public interface IEventProvider
{
Map<String, String> getNewEvents();
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* @author anttil
*/
public class ParameterMap
{
private final Map<String, String> map;
private final IEventProvider eventProvider;
public ParameterMap(IEventProvider eventProvider)
{
this.eventProvider = eventProvider;
map = new HashMap<String, String>();
}
public void addParameter(String key, String defaultValue)
{
map.put(key, defaultValue);
}
public String get(String key)
{
Map<String, String> newEvents = eventProvider.getNewEvents();
map.putAll(newEvents);
return map.get(key);
}
public static void main(String args[])
{
ParameterMap map = new ParameterMap(
new DelayingDecorator(5000,
new FileSystemBasedEventProvider(new File("/tmp/test"))));
map.addParameter("parameter", "100");
while (true)
{
System.out.println(map.get("parameter"));
try
{
Thread.sleep(100);
} catch (InterruptedException ex)
{
}
}
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
/**
* @author anttil
*/
class SystemClock implements IClock
{
@Override
public long getTime()
{
return System.currentTimeMillis();
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author anttil
*/
public class DelayingDecoratorTest
{
Mockery context;
private MockClock clock;
private IEventProvider provider;
private IEventProvider decorator;
private static final long INTERVAL = 500;
@BeforeMethod
public void fixture()
{
clock = new MockClock(System.currentTimeMillis());
context = new Mockery();
provider = context.mock(IEventProvider.class);
decorator = new DelayingDecorator(INTERVAL, provider, clock);
}
@Test
public void eventsAreDelayed() throws Exception
{
context.checking(new Expectations()
{
{
Map<String, String> updates = new HashMap<String, String>();
updates.put("parameter", "update");
exactly(2).of(provider).getNewEvents();
will(returnValue(updates));
}
});
assertThat(decorator.getNewEvents().size(), is(1));
clock.setTime(clock.getTime() + INTERVAL - 1);
assertThat(decorator.getNewEvents().size(), is(0));
clock.setTime(clock.getTime() + INTERVAL);
assertThat(decorator.getNewEvents().size(), is(1));
context.assertIsSatisfied();
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.io.File;
import java.util.Map;
import java.util.UUID;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author anttil
*/
public class FileSystemBasedEventProviderTest
{
File controlDir;
private IEventProvider provider;
@BeforeMethod
public void fixture()
{
controlDir = new File("/tmp/" + UUID.randomUUID().toString());
controlDir.mkdir();
provider = new FileSystemBasedEventProvider(controlDir);
}
@Test
public void eventsAreReturnedOnlyOnce() throws Exception
{
new File(controlDir, "parameter-x").createNewFile();
assertThat(provider.getNewEvents().isEmpty(), is(false));
assertThat(provider.getNewEvents().isEmpty(), is(true));
}
@Test
public void keyAndValueAreParsedCorrectlyFromTheFileName() throws Exception
{
new File(controlDir, "parameter-x").createNewFile();
Map<String, String> events = provider.getNewEvents();
assertThat(events.get("parameter"), is("x"));
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
/**
* @author anttil
*/
public class MockClock implements IClock
{
private long time;
public MockClock(long time)
{
this.time = time;
}
public void setTime(long time)
{
this.time = time;
}
@Override
public long getTime()
{
return time;
}
}
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed 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 ch.systemsx.cisd.common.filesystem.control;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author anttil
*/
public class ParameterMapTest
{
IEventProvider eventProvider;
Mockery context;
ParameterMap map;
@BeforeMethod
public void fixture()
{
context = new Mockery();
eventProvider = context.mock(IEventProvider.class);
map = new ParameterMap(eventProvider);
}
@Test
public void registeredParametersReturnDefaultValue() throws Exception
{
context.checking(new Expectations()
{
{
allowing(eventProvider).getNewEvents();
will(returnValue(new HashMap<String, String>()));
}
});
map.addParameter("parameter", "value");
assertThat(map.get("parameter"), is("value"));
}
@Test
public void parametersCanBeUpdated() throws Exception
{
context.checking(new Expectations()
{
{
allowing(eventProvider).getNewEvents();
will(returnValue(getUpdateOn("parameter")));
}
});
map.addParameter("parameter", "default value");
assertThat(map.get("parameter"), is("updated value"));
}
private Map<String, String> getUpdateOn(String... keys)
{
Map<String, String> updates = new HashMap<String, String>();
for (String key : keys)
{
updates.put(key, "updated value");
}
return updates;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment