Esempio n. 1
0
 /**
  * Run with given args
  *
  * @param   string[] args
  * @param   string in
  * @param   util.PropertySource[] propertySources default array()
  * @return  int
  */
 protected function runWith(array $args, $in = '', $propertySources = array())
 {
     $pm = PropertyManager::getInstance();
     $sources = $pm->getSources();
     $pm->setSources($propertySources);
     $this->in = $this->runner->setIn(new MemoryInputStream($in));
     $this->out = $this->runner->setOut(new MemoryOutputStream());
     $this->err = $this->runner->setErr(new MemoryOutputStream());
     try {
         $res = $this->runner->run(new ParamString($args));
         $pm->setSources($sources);
         return $res;
     } catch (Throwable $t) {
         $pm->setSources($sources);
         throw $t;
     }
 }
Esempio n. 2
0
 /**
  * Main method
  *
  * @param   util.cmd.ParamString params
  * @return  int
  */
 public function run(ParamString $params)
 {
     // No arguments given - show our own usage
     if ($params->count < 1) {
         self::$err->writeLine(self::textOf(XPClass::forName(xp::nameOf(__CLASS__))->getComment()));
         return 1;
     }
     // Configure properties
     $pm = PropertyManager::getInstance();
     // Separate runner options from class options
     for ($offset = 0, $i = 0; $i < $params->count; $i++) {
         switch ($params->list[$i]) {
             case '-c':
                 if (0 == strncmp('res://', $params->list[$i + 1], 6)) {
                     $pm->appendSource(new ResourcePropertySource(substr($params->list[$i + 1], 6)));
                 } else {
                     $pm->appendSource(new FilesystemPropertySource($params->list[$i + 1]));
                 }
                 $offset += 2;
                 $i++;
                 break;
             case '-cp':
                 ClassLoader::registerPath($params->list[$i + 1], NULL);
                 $offset += 2;
                 $i++;
                 break;
             case '-v':
                 $this->verbose = TRUE;
                 $offset += 1;
                 $i++;
                 break;
             default:
                 break 2;
         }
     }
     // Sanity check
     if (!$params->exists($offset)) {
         self::$err->writeLine('*** Missing classname');
         return 1;
     }
     // Use default path for PropertyManager if no sources set
     if (!$pm->getSources()) {
         $pm->configure(self::DEFAULT_CONFIG_PATH);
     }
     unset($params->list[-1]);
     $classname = $params->value($offset);
     $classparams = new ParamString(array_slice($params->list, $offset + 1));
     // Class file or class name
     if (strstr($classname, xp::CLASS_FILE_EXT)) {
         $file = new File($classname);
         if (!$file->exists()) {
             self::$err->writeLine('*** Cannot load class from non-existant file ', $classname);
             return 1;
         }
         $uri = $file->getURI();
         $path = dirname($uri);
         $paths = array_flip(array_map('realpath', xp::$classpath));
         $class = NULL;
         while (FALSE !== ($pos = strrpos($path, DIRECTORY_SEPARATOR))) {
             if (isset($paths[$path])) {
                 $class = XPClass::forName(strtr(substr($uri, strlen($path) + 1, -10), DIRECTORY_SEPARATOR, '.'));
                 break;
             }
             $path = substr($path, 0, $pos);
         }
         if (!$class) {
             self::$err->writeLine('*** Cannot load class from ', $file);
             return 1;
         }
     } else {
         try {
             $class = XPClass::forName($classname);
         } catch (ClassNotFoundException $e) {
             self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage());
             return 1;
         }
     }
     // Check whether class is runnable
     if (!$class->isSubclassOf('lang.Runnable')) {
         self::$err->writeLine('*** ', $class->getName(), ' is not runnable');
         return 1;
     }
     // Usage
     if ($classparams->exists('help', '?')) {
         self::showUsage($class);
         return 0;
     }
     // Load, instantiate and initialize
     $l = Logger::getInstance();
     $pm->hasProperties('log') && $l->configure($pm->getProperties('log'));
     $cm = ConnectionManager::getInstance();
     $pm->hasProperties('database') && $cm->configure($pm->getProperties('database'));
     // Setup logger context for all registered log categories
     foreach (Logger::getInstance()->getCategories() as $category) {
         if (NULL === ($context = $category->getContext()) || !$context instanceof EnvironmentAware) {
             continue;
         }
         $context->setHostname(System::getProperty('host.name'));
         $context->setRunner($this->getClassName());
         $context->setInstance($class->getName());
         $context->setResource(NULL);
         $context->setParams($params->string);
     }
     $instance = $class->newInstance();
     $instance->in = self::$in;
     $instance->out = self::$out;
     $instance->err = self::$err;
     $methods = $class->getMethods();
     // Injection
     foreach ($methods as $method) {
         if (!$method->hasAnnotation('inject')) {
             continue;
         }
         $inject = $method->getAnnotation('inject');
         if (isset($inject['type'])) {
             $type = $inject['type'];
         } else {
             if ($restriction = $method->getParameter(0)->getTypeRestriction()) {
                 $type = $restriction->getName();
             } else {
                 $type = $method->getParameter(0)->getType()->getName();
             }
         }
         try {
             switch ($type) {
                 case 'rdbms.DBConnection':
                     $args = array($cm->getByHost($inject['name'], 0));
                     break;
                 case 'util.Properties':
                     $p = $pm->getProperties($inject['name']);
                     // If a PropertyAccess is retrieved which is not a util.Properties,
                     // then, for BC sake, convert it into a util.Properties
                     if ($p instanceof PropertyAccess && !$p instanceof Properties) {
                         $convert = Properties::fromString('');
                         $section = $p->getFirstSection();
                         while ($section) {
                             // HACK: Properties::writeSection() would first attempts to
                             // read the whole file, we cannot make use of it.
                             $convert->_data[$section] = $p->readSection($section);
                             $section = $p->getNextSection();
                         }
                         $args = array($convert);
                     } else {
                         $args = array($p);
                     }
                     break;
                 case 'util.log.LogCategory':
                     $args = array($l->getCategory($inject['name']));
                     break;
                 default:
                     self::$err->writeLine('*** Unknown injection type "' . $type . '" at method "' . $method->getName() . '"');
                     return 2;
             }
             $method->invoke($instance, $args);
         } catch (TargetInvocationException $e) {
             self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->getCause()->compoundMessage());
             return 2;
         } catch (Throwable $e) {
             self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->compoundMessage());
             return 2;
         }
     }
     // Arguments
     foreach ($methods as $method) {
         if ($method->hasAnnotation('args')) {
             // Pass all arguments
             if (!$method->hasAnnotation('args', 'select')) {
                 $begin = 0;
                 $end = $classparams->count;
                 $pass = array_slice($classparams->list, 0, $end);
             } else {
                 $pass = array();
                 foreach (preg_split('/, ?/', $method->getAnnotation('args', 'select')) as $def) {
                     if (is_numeric($def) || '-' == $def[0]) {
                         $pass[] = $classparams->value((int) $def);
                     } else {
                         sscanf($def, '[%d..%d]', $begin, $end);
                         isset($begin) || ($begin = 0);
                         isset($end) || ($end = $classparams->count - 1);
                         while ($begin <= $end) {
                             $pass[] = $classparams->value($begin++);
                         }
                     }
                 }
             }
             try {
                 $method->invoke($instance, array($pass));
             } catch (Throwable $e) {
                 self::$err->writeLine('*** Error for arguments ' . $begin . '..' . $end . ': ', $this->verbose ? $e : $e->getMessage());
                 return 2;
             }
         } else {
             if ($method->hasAnnotation('arg')) {
                 // Pass arguments
                 $arg = $method->getAnnotation('arg');
                 if (isset($arg['position'])) {
                     $name = '#' . ($arg['position'] + 1);
                     $select = intval($arg['position']);
                     $short = NULL;
                 } else {
                     if (isset($arg['name'])) {
                         $name = $select = $arg['name'];
                         $short = isset($arg['short']) ? $arg['short'] : NULL;
                     } else {
                         $name = $select = strtolower(preg_replace('/^set/', '', $method->getName()));
                         $short = isset($arg['short']) ? $arg['short'] : NULL;
                     }
                 }
                 if (0 == $method->numParameters()) {
                     if (!$classparams->exists($select, $short)) {
                         continue;
                     }
                     $args = array();
                 } else {
                     if (!$classparams->exists($select, $short)) {
                         list($first, ) = $method->getParameters();
                         if (!$first->isOptional()) {
                             self::$err->writeLine('*** Argument ' . $name . ' does not exist!');
                             return 2;
                         }
                         $args = array();
                     } else {
                         $args = array($classparams->value($select, $short));
                     }
                 }
                 try {
                     $method->invoke($instance, $args);
                 } catch (TargetInvocationException $e) {
                     self::$err->writeLine('*** Error for argument ' . $name . ': ', $this->verbose ? $e->getCause() : $e->getCause()->compoundMessage());
                     return 2;
                 }
             }
         }
     }
     try {
         $instance->run();
     } catch (Throwable $t) {
         self::$err->writeLine('*** ', $t->toString());
         return 70;
         // EX_SOFTWARE according to sysexits.h
     }
     return 0;
 }
 public function isSingleton()
 {
     $this->assertEquals(PropertyManager::getInstance()->hashCode(), PropertyManager::getInstance()->hashCode());
 }
Esempio n. 4
0
 /**
  * Returns arguments used for injection 
  *
  * @param  lang.reflect.Routine routine
  * @return var[] args
  */
 protected function injectionArgs($routine)
 {
     if ($routine->numParameters() < 1) {
         return array();
     }
     $inject = $routine->getAnnotation('inject');
     $type = isset($inject['type']) ? $inject['type'] : $routine->getParameter(0)->getType()->getName();
     switch ($type) {
         case 'util.log.LogCategory':
             $args = array(isset($inject['name']) ? Logger::getInstance()->getCategory($inject['name']) : $this->cat);
             break;
         case 'util.Properties':
             $args = array(PropertyManager::getInstance()->getProperties($inject['name']));
             break;
         case 'webservices.rest.srv.RestContext':
             $args = array($this);
             break;
         default:
             throw new IllegalStateException('Unkown injection type ' . $type);
     }
     return $args;
 }
 public function setter_injection()
 {
     $prop = new Properties('service.ini');
     PropertyManager::getInstance()->register('service', $prop);
     $class = ClassLoader::defineClass('AbstractRestRouterTest_SetterInjection', 'lang.Object', array(), '{
     public $prop;
     #[@inject(type = "util.Properties", name = "service")]
     public function setServiceConfig($prop) { $this->prop= $prop; }
   }');
     $this->assertEquals($prop, $this->fixture->handlerInstanceFor($class)->prop);
 }
 /**
  * Fetch Properties resource
  *
  * @param   string name
  */
 function getProperties($name)
 {
     return PropertyManager::getInstance()->getProperties($name);
 }
Esempio n. 7
0
 /**
  * Creates the scriptlet instance for the given URL and runs it
  *
  * @param   string url default '/'
  */
 public function run($url = '/')
 {
     // Determine which scriptlet should be run
     $application = $this->applicationAt($url);
     // Determine debug level
     $flags = $application->getDebug();
     // Initializer logger, properties and connections to property base,
     // defaulting to the same directory the web.ini resides in
     $pm = PropertyManager::getInstance();
     foreach (explode('|', $application->getConfig()) as $element) {
         $expanded = $this->expand($element);
         if (0 == strncmp('res://', $expanded, 6)) {
             $pm->appendSource(new ResourcePropertySource($expanded));
         } else {
             $pm->appendSource(new FilesystemPropertySource($expanded));
         }
     }
     $l = Logger::getInstance();
     $pm->hasProperties('log') && $l->configure($pm->getProperties('log'));
     $cm = ConnectionManager::getInstance();
     $pm->hasProperties('database') && $cm->configure($pm->getProperties('database'));
     // Setup logger context for all registered log categories
     foreach (Logger::getInstance()->getCategories() as $category) {
         if (NULL === ($context = $category->getContext()) || !$context instanceof EnvironmentAware) {
             continue;
         }
         $context->setHostname($_SERVER['SERVER_NAME']);
         $context->setRunner($this->getClassName());
         $context->setInstance($application->getScriptlet());
         $context->setResource($url);
         $context->setParams($_SERVER['QUERY_STRING']);
     }
     // Set environment variables
     foreach ($application->getEnvironment() as $key => $value) {
         $_SERVER[$key] = $this->expand($value);
     }
     // Instantiate and initialize
     $cat = $l->getCategory('scriptlet');
     $instance = NULL;
     $e = NULL;
     try {
         $class = XPClass::forName($application->getScriptlet());
         if (!$class->hasConstructor()) {
             $instance = $class->newInstance();
         } else {
             $args = array();
             foreach ($application->getArguments() as $arg) {
                 $args[] = $this->expand($arg);
             }
             $instance = $class->getConstructor()->newInstance($args);
         }
         if ($flags & WebDebug::TRACE && $instance instanceof Traceable) {
             $instance->setTrace($cat);
         }
         $instance->init();
         // Service
         $response = $instance->process();
     } catch (ScriptletException $e) {
         $cat->error($e);
         // TODO: Instead of checking for a certain method, this should
         // check if the scriptlet class implements a certain interface
         if (method_exists($instance, 'fail')) {
             $response = $instance->fail($e);
         } else {
             $response = $this->fail($e, $e->getStatus(), $flags & WebDebug::STACKTRACE);
         }
     } catch (SystemExit $e) {
         if (0 === $e->getCode()) {
             $response = new HttpScriptletResponse();
             $response->setStatus(HttpConstants::STATUS_OK);
             if ($message = $e->getMessage()) {
                 $response->setContent($message);
             }
         } else {
             $cat->error($e);
             $response = $this->fail($e, HttpConstants::STATUS_INTERNAL_SERVER_ERROR, FALSE);
         }
     } catch (Throwable $e) {
         $cat->error($e);
         // Here, we might not have a scriptlet
         $response = $this->fail($e, HttpConstants::STATUS_PRECONDITION_FAILED, $flags & WebDebug::STACKTRACE);
     }
     // Send output
     $response->isCommitted() || $response->flush();
     $response->sendContent();
     // Call scriptlet's finalizer
     $instance && $instance->finalize();
     // Debugging
     if ($flags & WebDebug::XML && isset($response->document)) {
         flush();
         echo '<xmp>', $response->document->getDeclaration() . "\n" . $response->document->getSource(0), '</xmp>';
     }
     if ($flags & WebDebug::ERRORS) {
         flush();
         echo '<xmp>', $e ? $e->toString() : '', xp::stringOf(xp::$errors), '</xmp>';
     }
 }
Esempio n. 8
0
 public static function removePropertySource()
 {
     PropertyManager::getInstance()->removeSource(self::$propertySource);
 }