Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 org.apache.uima.ducc.test.randomsleep;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import org.apache.uima.UIMAFramework;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_component.CasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.examples.SourceDocumentInformation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.apache.uima.util.Logger;
/**
* Simple AE for the system test. It does no computation, instead sleeping to simulate computation. It
* is able to inject errors and adjust it's simulated initialization time.
*/
public class FixedSleepAE extends CasAnnotator_ImplBase
{
Random r;
Logger logger;
static boolean initComplete = false;
String AE_Identifier = "*^^^^^^^^^ AE ";
ArrayList< long[] > bloated_space = new ArrayList< long[] >();
@Override
public void initialize(UimaContext uimaContext) throws ResourceInitializationException
{
super.initialize(uimaContext);
long tid = Thread.currentThread().getId();
Map env = System.getenv();
RuntimeMXBean rmxb = ManagementFactory.getRuntimeMXBean();
String pid = rmxb.getName();
long seed = System.currentTimeMillis();
r = new Random(seed);
logger = UIMAFramework.getLogger(FixedSleepAE.class);
if ( logger == null ) {
System.out.println("Is this nuts or what, no logger!");
}
if ( initComplete ) {
logger.log(Level.INFO, "Init bypassed in PID:TID " + pid + ":" + tid + ", already completed. ");
return;
} else {
if ( logger != null )
logger.log(Level.INFO, "Init procedes in PID:TIDs " + pid + ":" + tid + " Environment:");
for ( String k : env.keySet() ) {
if ( logger != null )
logger.log(Level.INFO, String.format("Environment[%s] = %s", k, env.get(k)));
}
File workingdir = new File(System.getProperty("user.dir"));
File[] files = workingdir.listFiles();
if ( logger != null )
logger.log(Level.INFO, "Working directory is " + workingdir.toString());
if ( files != null ) {
for ( File f : files ) {
if ( logger != null )
logger.log(Level.INFO, "File: " + f.toString());
}
}
}
long sleep;
if ( !initComplete ) { // longer init only the first tim
initComplete = true;
}
int i_error = getIntFromEnv("AE_INIT_ERROR", false); // probability of init error, int, 0:100
int i_exit = getIntFromEnv("AE_INIT_EXIT" , false);
int i_itime = getIntFromEnv("AE_INIT_TIME" , true );
int i_irange = getIntFromEnv("AE_INIT_RANGE", true );
if ( i_error > 0 ) {
int toss = nextrand(100);
if ( logger != null )
logger.log(Level.INFO, "Init errors: probability[" + i_error + "] toss[" + toss + "]");
if ( i_error > toss ) {
throwAnException("Random Error in Initialization");
}
}
if ( i_exit > 0 ) {
int toss = nextrand(100);
if ( logger != null )
logger.log(Level.INFO, "Init hard exit: probability[" + i_exit + "] toss[" + toss + "]");
if ( i_exit > toss ) {
if ( logger != null )
logger.log(Level.INFO, "Init hard exit: croaking hard now.");
Runtime.getRuntime().halt(0);
}
}
if ( i_itime < 0 ) {
throw new IllegalArgumentException("Invalid AE_INIT_TIME, must be >= 0");
}
if ( i_irange <= 0 ) {
throw new IllegalArgumentException("Invalid AE_INIT_RANGE, must be > 0");
}
sleep = i_itime + nextrand(i_irange); // pick off some random number of milliseconds, min of 5 minutes init sleep
if ( logger != null )
logger.log(Level.INFO, "^^--------> Initialization sleep time is " + sleep + " milliseconds");
String bloat = System.getenv("INIT_BLOAT");
if ( bloat != null ) {
if ( logger != null )
logger.log(Level.INFO, "INIT_BLOAT is set to " + bloat + "; starting bloat in init");
runBloater(bloat);
}
String ok = "INTERRUPTED";
if ( logger != null )
logger.log(Level.INFO, "^^-------> AE process " + pid + " TID " + tid + " initialization starts: sleep " + sleep + "MS");
try {
Thread.sleep(sleep);
ok = "OK";
} catch (InterruptedException e) {
if ( logger != null )
logger.log(Level.INFO, "^^-------> AE process " + pid + " TID " + tid + " my sleep has been disturbed!");
}
if ( logger != null )
logger.log(Level.INFO, "^^-------> AE process " + pid + " TID " + tid + " initialization " + ok);
return;
}
int getIntFromEnv(String key, boolean fail)
{
String s = System.getenv(key);
if ( s == null ) {
if ( fail ) throw new IllegalArgumentException("Missing " + key);
else return 0;
}
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
if ( logger != null )
logger.log(Level.INFO, "Invalid " + key + "[" + s + "]. Must be integer.");
throw e;
}
}
double getDoubleFromEnv(String key, boolean fail)
{
String s = System.getenv(key);
if ( s == null ) {
if ( fail ) throw new IllegalArgumentException("Missing " + key);
else return 0.0;
}
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
if ( logger != null )
logger.log(Level.INFO, "Invalid " + key + "[" + s + "]. Must be double.");
throw e;
}
}
/**
* Need to simulate a process that leaks. We just allocate stuff until we die somehow.
* Careful, this can be pretty nasty if not contained by the infrastructure.
*
* Older code = use the Bloater class for better results.
*/
void runBloater(String gb)
{
HashMap