| <!-- |
| 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. |
| --> |
| <body> |
| <h1>Step 2 - Hooking into the Service Activation</h1> |
| |
| <p> |
| Services can be "<i>activated</i>" and "<i>passivated</i>". Applications can be notified of this occurring |
| by Zestâ„¢ runtime by assembling them with an <code>Activator</code>. |
| </p> |
| <p> |
| Activators methods are called around "<i>activation</i>" and "<i>passivation</i>": <code>beforeActivation</code>, |
| <code>afterActivation</code>, <code>beforeActivation</code>, <code>afterPassivation</code>. The |
| <code>ActivatorAdapter</code> class help you keeping your code short when you only need one or two hooks. |
| </p> |
| <p> |
| To showcase how this works, we refactor the code to create a number of copies of the books, to be lend out |
| upon call to the <code>borrowBook</code> method, which will return null if no copy is available. The book |
| copies are created in the <code>activate</code> method. |
| </p> |
| |
| <p>Steps to do.</p> |
| <ol> |
| <li> |
| Add a <code>createInitialData</code> method to <code>Library</code>. |
| </li> |
| <li> |
| In the implementation, create a couple of books of each title and store each copy in a |
| <code>HashMap<String,ArrayList<Book>></code>. |
| </li> |
| <li> |
| Write an <code>Activator<ServiceReference<Library>></code> class extending |
| <code>ActivatorAdapter</code>. |
| </li> |
| <li> |
| Override the <code>afterActivation</code> method, use the <code>ServiceReference</code> to get a |
| handle on the <code>Library</code> and call its <code>createInitialData</code> method. |
| </li> |
| <li> |
| Add the <code>@Activators</code> annotation to the <code>LibraryService</code> declaring the new |
| <code>LibraryActivator</code>. |
| </li> |
| </ol> |
| |
| </body> |