Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Andrey Adamovich - Groovy 2 Cookbook - 2013.pdf
Скачиваний:
44
Добавлен:
19.03.2016
Размер:
26.28 Mб
Скачать

www.PacktPub.com

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

TM

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.

Why Subscribe?

ff

ff

ff

Fully searchable across every book published by Packt Copy and paste, print and bookmark content

On demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

www.it-ebooks.info

www.it-ebooks.info

Table of Contents

Preface

1

Chapter 1: Getting Started with Groovy

7

Introduction

7

Installing Groovy on Windows

8

Installing Groovy on Linux and OS X

10

Executing Groovy code from the command line

11

Using Groovy as a command-line text file editor

14

Using Groovy to start a server on the command line

16

Running Groovy with invokedynamic support

17

Building Groovy from source

18

Managing multiple Groovy installations on Linux

20

Using groovysh to try out Groovy commands

23

Starting groovyConsole to execute Groovy snippets

29

Configuring Groovy in Eclipse

35

Configuring Groovy in IntelliJ IDEA

42

Chapter 2: Using Groovy Ecosystem

47

Introduction

47

Using Java classes from Groovy

48

Embedding Groovy into Java

51

Compiling Groovy code

57

Simplifying dependency management with Grape

60

Integrating Groovy into the build process using Ant

63

Integrating Groovy into the build process using Maven

71

Integrating Groovy into the build process using Gradle

75

Generating documentation for Groovy code

78

Checking Groovy code's quality with CodeNarc

82

www.it-ebooks.info

Table of Contents

Chapter 3: Using Groovy Language Features

89

Introduction

89

Searching strings with regular expressions

90

Writing less verbose Java Beans with Groovy Beans

92

Inheriting constructors in Groovy classes

99

Adding the cloning functionality to Groovy Beans

100

Defining code as data in Groovy

104

Defining data structures as code in Groovy

108

Implementing multiple inheritance in Groovy

117

Adding a functionality to the existing Java/Groovy classes

123

Defining type-checking rules for dynamic code

126

Adding automatic logging to Groovy classes

132

Chapter 4: Working with Files in Groovy

135

Introduction

136

Reading from a file

136

Reading a text file line by line

138

Processing every word in a text file

140

Writing to a file

142

Replacing tabs with spaces in a text file

144

Filtering a text file's content

146

Deleting a file or directory

149

Walking through a directory recursively

151

Searching for files

153

Changing file attributes on Windows

157

Reading data from a ZIP file

159

Reading an Excel file

160

Extracting data from a PDF

162

Chapter 5: Working with XML in Groovy

167

Introduction

167

Reading XML using XmlSlurper

168

Reading XML using XmlParser

170

Reading XML content with namespaces

172

Searching in XML with GPath

175

Searching in XML with XPath

178

Constructing XML content

184

Modifying XML content

187

Sorting XML nodes

189

Serializing Groovy Beans to XML

192

ii

www.it-ebooks.info

 

Table of Contents

 

Chapter 6: Working with JSON in Groovy

197

Introduction

197

Parsing JSON messages with JsonSlurper

198

Constructing JSON messages with JsonBuilder

201

Modifying JSON messages

204

Validating JSON messages

205

Converting JSON message to XML

211

Converting JSON message to Groovy Bean

214

Using JSON to configure your scripts

218

Chapter 7: Working with Databases in Groovy

223

Introduction

223

Creating a database table

224

Connecting to an SQL database

228

Querying an SQL database

231

Modifying data in an SQL database

235

Calling a stored procedure

239

Reading BLOB/CLOB from a database

242

Building a simple ORM framework

244

Using Groovy to access Redis

246

Using Groovy to access MongoDB

252

Using Groovy to access Apache Cassandra

256

Chapter 8: Working with Web Services in Groovy

263

Introduction

263

Downloading content from the Internet

264

Executing an HTTP GET request

265

Executing an HTTP POST request

268

Constructing and modifying complex URLs

271

Issuing a REST request and parsing a response

272

Issuing a SOAP request and parsing a response

276

Consuming RSS and Atom feeds

279

Using basic authentication for web service security

283

Using OAuth for web service security

284

Chapter 9: Metaprogramming and DSLs in Groovy

289

Introduction

289

Querying methods and properties

290

Dynamically extending classes with new methods

293

Overriding methods dynamically

296

Adding performance logging to methods

299

iii

www.it-ebooks.info

Table of Contents

 

 

 

 

Adding a caching functionality around methods

305

Adding transparent imports to a script

317

DSL for executing commands over SSH

319

DSL for generating reports from logfiles

327

Chapter 10: Concurrent Programming in Groovy

335

Introduction

335

Processing collections concurrently

336

Downloading files concurrently

340

Splitting a large task into smaller parallel jobs

344

Running tasks in parallel and asynchronously

349

Using actors to build message-based concurrency

355

Using STM to atomically update fields

361

Using dataflow variables for lazy evaluation

365

Index

371

iv

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]