/**
  * Returns an instance with a given number of DSNs
  *
  * @param   [:string] dsns
  * @return  rdbms.ConnectionManager
  */
 protected function instanceWith($dsns)
 {
     $properties = '';
     foreach ($dsns as $name => $dsn) {
         $properties .= '[' . $name . "]\ndsn=\"" . $dsn . "\"\n";
     }
     $p = new Properties(null);
     $p->load(new MemoryInputStream($properties));
     $cm = ConnectionManager::getInstance();
     $cm->configure($p);
     return $cm;
 }
Пример #2
0
 /**
  * Main
  *
  * Exitcodes used:
  * <ul>
  *   <li>127: Archive referenced in -xar [...] does not exist</li>
  *   <li>126: No manifest or manifest does not have a main-class</li>
  * </ul>
  *
  * @see     http://tldp.org/LDP/abs/html/exitcodes.html
  * @param   string[] args
  * @return  int
  */
 public static function main(array $args)
 {
     // Open archive
     $f = new File(array_shift($args));
     if (!$f->exists()) {
         Console::$err->writeLine('*** Cannot find archive ' . $f->getURI());
         return 127;
     }
     // Register class loader
     $cl = \lang\ClassLoader::registerLoader(new \lang\archive\ArchiveClassLoader(new Archive($f)));
     if (!$cl->providesResource(self::MANIFEST)) {
         Console::$err->writeLine('*** Archive ' . $f->getURI() . ' does not have a manifest');
         return 126;
     }
     // Load manifest
     $pr = Properties::fromString($cl->getResource(self::MANIFEST));
     if (null === ($class = $pr->readString('archive', 'main-class', null))) {
         Console::$err->writeLine('*** Archive ' . $f->getURI() . '\'s manifest does not have a main class');
         return 126;
     }
     // Run main()
     try {
         return \lang\XPClass::forName($class, $cl)->getMethod('main')->invoke(null, [$args]);
     } catch (\lang\reflect\TargetInvocationException $e) {
         throw $e->getCause();
     }
 }
 public function propertiesFromSameFileAreEqual()
 {
     $one = Properties::fromFile($this->getClass()->getPackage()->getResourceAsStream('example.ini'));
     $two = Properties::fromFile($this->getClass()->getPackage()->getResourceAsStream('example.ini'));
     $this->assertFalse($one === $two);
     $this->assertTrue($one->equals($two));
 }
 /**
  * Returns an instance with a given number of DSNs
  *
  * @param   [:string] dsns
  * @return  rdbms.ConnectionManager
  */
 protected function instanceWith($dsns)
 {
     $properties = '';
     foreach ($dsns as $name => $dsn) {
         $properties .= '[' . $name . "]\ndsn=\"" . $dsn . "\"\n";
     }
     $cm = \rdbms\ConnectionManager::getInstance();
     $cm->configure(Properties::fromString($properties));
     return $cm;
 }
 /**
  * Set up testcase
  */
 public function setUp()
 {
     $this->dsn = \util\Properties::fromString($this->getClass()->getPackage()->getResource('database.ini'))->readString($this->_dsn(), 'dsn', null);
     if (null === $this->dsn) {
         throw new \unittest\PrerequisitesNotMetError('No credentials for ' . $this->getClassName());
     }
     try {
         $this->conn = DriverManager::getConnection($this->dsn);
     } catch (\lang\Throwable $t) {
         throw new \unittest\PrerequisitesNotMetError($t->getMessage(), $t);
     }
 }
Пример #6
0
 /**
  * Configures logger
  *
  * @param  string $properties
  * @return void
  */
 private function configure($properties)
 {
     $p = new Properties(null);
     $p->load(new MemoryInputStream(trim($properties)));
     $this->logger->configure($p);
 }
Пример #7
0
    public function configureWithContext()
    {
        $this->logger->configure(\util\Properties::fromString(trim('
[context]
appenders="util.log.FileAppender"
context="util.log.context.NestedLogContext"
appender.util.log.FileAppender.params="filename"
appender.util.log.FileAppender.param.filename="/var/log/xp/default.log"
    ')));
        with($cat = $this->logger->getCategory('context'));
        $this->assertTrue($cat->hasContext());
        $this->assertInstanceOf('util.log.context.NestedLogContext', $cat->getContext());
    }
Пример #8
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) {
         return self::usage();
     }
     // 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':
                 \lang\ClassLoader::registerPath($params->list[$i + 1], null);
                 $offset += 2;
                 $i++;
                 break;
             case '-v':
                 $this->verbose = true;
                 $offset += 1;
                 $i++;
                 break;
             case '-?':
                 return self::usage();
             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 \io\File($classname);
         if (!$file->exists()) {
             self::$err->writeLine('*** Cannot load class from non-existant file ', $classname);
             return 1;
         }
         try {
             $class = \lang\ClassLoader::getDefault()->loadUri($file->getURI());
         } catch (\lang\ClassNotFoundException $e) {
             self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage());
             return 1;
         }
     } else {
         try {
             $class = \lang\XPClass::forName($classname);
         } catch (\lang\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'));
     if (class_exists('rdbms\\DBConnection')) {
         // FIXME: Job of XPInjector?
         $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(\lang\System::getProperty('host.name'));
         $context->setRunner(nameof($this));
         $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 = [$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 \util\PropertyAccess && !$p instanceof \util\Properties) {
                         $convert = \util\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 = [$convert];
                     } else {
                         $args = [$p];
                     }
                     break;
                 case 'util.log.LogCategory':
                     $args = [$l->getCategory($inject['name'])];
                     break;
                 default:
                     self::$err->writeLine('*** Unknown injection type "' . $type . '" at method "' . $method->getName() . '"');
                     return 2;
             }
             $method->invoke($instance, $args);
         } catch (\lang\reflect\TargetInvocationException $e) {
             self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->getCause()->compoundMessage());
             return 2;
         } catch (\lang\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 = [];
                 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, [$pass]);
             } catch (\lang\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 = [];
                 } else {
                     if (!$classparams->exists($select, $short)) {
                         list($first, ) = $method->getParameters();
                         if (!$first->isOptional()) {
                             self::$err->writeLine('*** Argument ' . $name . ' does not exist!');
                             return 2;
                         }
                         $args = [];
                     } else {
                         $args = [$classparams->value($select, $short)];
                     }
                 }
                 try {
                     $method->invoke($instance, $args);
                 } catch (\lang\reflect\TargetInvocationException $e) {
                     self::$err->writeLine('*** Error for argument ' . $name . ': ', $this->verbose ? $e->getCause() : $e->getCause()->compoundMessage());
                     return 2;
                 }
             }
         }
     }
     try {
         $instance->run();
     } catch (\lang\Throwable $t) {
         self::$err->writeLine('*** ', $t->toString());
         return 70;
         // EX_SOFTWARE according to sysexits.h
     }
     return 0;
 }
 public function addingToCompositeResetsIterationPointer()
 {
     $fixture = $this->getThirdSection();
     $fixture->add(Properties::fromString('[unknown]'));
     $this->assertEquals(null, $fixture->getNextSection());
 }
 /**
  * Creates a new properties object from a string
  *
  * @param   string source
  * @return  util.Properties
  */
 private function newProperties($source)
 {
     $prop = new Properties(null);
     $prop->load(new MemoryInputStream(preg_replace('/^\\s*/', '', $source)));
     return $prop;
 }
Пример #11
0
    public function memoryPropertiesAlwaysHavePrecendenceInCompositeProperties()
    {
        $fixture = $this->preconfigured();
        $this->assertEquals('value', $fixture->getProperties('example')->readString('section', 'key'));
        $fixture->register('example', Properties::fromString('[section]
key="overwritten value"'));
        $this->assertEquals('overwritten value', $fixture->getProperties('example')->readString('section', 'key'));
    }
 /**
  * Create a new properties object from a string source
  *
  * @param   string source
  * @return  util.Properties
  */
 protected function newPropertiesFrom($source)
 {
     return Properties::fromString($source);
 }
 public function equalsReturnsTrueForSameInnerPropertiesAndName()
 {
     $p1 = new RegisteredPropertySource('name1', \util\Properties::fromString('[section]'));
     $p2 = new RegisteredPropertySource('name1', \util\Properties::fromString('[section]'));
     $this->assertEquals($p1, $p2);
 }
 public function throws_error_when_reading()
 {
     $p = new Properties('@@does-not-exist.ini@@');
     $p->readString('section', 'key');
 }
Пример #15
0
 /**
  * Runs class
  *
  * @param  string $command
  * @param  util.cmd.ParamString $params
  * @param  util.cmd.Config $config
  * @return int
  */
 protected function runCommand($command, $params, $config)
 {
     try {
         $class = Commands::named($command);
     } catch (Throwable $e) {
         self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage());
         return 1;
     }
     // Usage
     if ($params->exists('help', '?')) {
         $this->commandUsage($class);
         return 0;
     }
     // BC: PropertyManager, Logger, ConnectionManager instances
     $pm = PropertyManager::getInstance();
     $pm->setSources($config->sources());
     $l = Logger::getInstance();
     $pm->hasProperties('log') && $l->configure($pm->getProperties('log'));
     if (class_exists('rdbms\\DBConnection')) {
         // FIXME: Job of XPInjector?
         $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(nameof($this));
         $context->setInstance($class->getName());
         $context->setResource(null);
         $context->setParams($params->string);
     }
     if ($class->hasMethod('newInstance')) {
         $instance = $class->getMethod('newInstance')->invoke(null, [$config]);
     } else {
         if ($class->hasConstructor()) {
             $instance = $class->newInstance($config);
         } else {
             $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 = [$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 = new Properties(null);
                         $convert->load(new \io\streams\MemoryInputStream(''));
                         $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 = [$convert];
                     } else {
                         $args = [$p];
                     }
                     break;
                 case 'util.log.LogCategory':
                     $args = [$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 = $params->count;
                 $pass = array_slice($params->list, 0, $end);
             } else {
                 $pass = [];
                 foreach (preg_split('/, ?/', $method->getAnnotation('args', 'select')) as $def) {
                     if (is_numeric($def) || '-' == $def[0]) {
                         $pass[] = $params->value((int) $def);
                     } else {
                         sscanf($def, '[%d..%d]', $begin, $end);
                         isset($begin) || ($begin = 0);
                         isset($end) || ($end = $params->count - 1);
                         while ($begin <= $end) {
                             $pass[] = $params->value($begin++);
                         }
                     }
                 }
             }
             try {
                 $method->invoke($instance, [$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 (!$params->exists($select, $short)) {
                         continue;
                     }
                     $args = [];
                 } else {
                     if (!$params->exists($select, $short)) {
                         list($first, ) = $method->getParameters();
                         if (!$first->isOptional()) {
                             self::$err->writeLine('*** Argument ' . $name . ' does not exist!');
                             return 2;
                         }
                         $args = [];
                     } else {
                         $args = [$params->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 {
         return (int) $instance->run();
     } catch (Throwable $t) {
         self::$err->writeLine('*** ', $t->toString());
         return 70;
         // EX_SOFTWARE according to sysexits.h
     }
 }
 /** @return util.Properties */
 private function newProperties()
 {
     $p = new Properties(null);
     $p->load(new MemoryInputStream(''));
     return $p;
 }
Пример #17
0
    public function configureWithLayout()
    {
        $this->logger->configure(\util\Properties::fromString(trim('
[fixture]
appenders="util.log.FileAppender"
appender.util.log.FileAppender.params="filename"
appender.util.log.FileAppender.param.filename="/var/log/xp/default.log"
appender.util.log.FileAppender.layout="util.log.layout.PatternLayout|%m"
    ')));
        $this->assertInstanceOf('util.log.layout.PatternLayout', $this->logger->getCategory('fixture')->getAppenders()[0]->getLayout());
    }
 public function mappingWithoutCorrespondingSection()
 {
     with($p = \util\Properties::fromString(''));
     $p->writeSection('app');
     $p->writeString('app', 'map.service', '/service');
     create(new WebConfiguration($p))->mappedApplications();
 }
Пример #19
0
    public function injectCompositeProperties()
    {
        $command = newinstance(Command::class, [], '{

      #[@inject(name= "debug")]
      public function setTrace(\\util\\Properties $prop) {
        $this->out->write("Have ", $prop->readString("section", "key"));
      }

      public function run() {
        // Intentionally empty
      }
    }');
        $this->runWith([nameof($command)], '', [new \util\RegisteredPropertySource('debug', \util\Properties::fromString('[section]
key=overwritten_value')), new \util\FilesystemPropertySource(__DIR__)]);
        $this->assertEquals('', $this->err->getBytes());
        $this->assertEquals('Have overwritten_value', $this->out->getBytes());
    }
 /** @return util.Properties */
 private function loadProperties($input)
 {
     $p = new Properties(null);
     $p->load(new MemoryInputStream($input));
     return $p;
 }
Пример #21
0
 public function noApplication()
 {
     with($p = \util\Properties::fromString(''));
     $p->writeSection('app');
     $p->writeString('app', 'map.service', '/service');
     $p->writeSection('app::service');
     $r = new Runner('/htdocs');
     $r->configure($p);
     $r->applicationAt('/');
 }